- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个奇怪的问题。我一直在互联网上搜索一种又好又快的高斯模糊算法。我终于找到了!
所以我所做的就是在一个新项目中尝试它 - 它工作得很好。
当我打算将该类导入到我的主项目中并尝试它时,我得到了一个 System.OverflowException
。我发现这真的很奇怪,它在一个项目中有效,但在另一个项目中却无效。
算法如下:
(我调用的函数几乎是一样的。我尝试以完全相同的方式调用它,但我仍然遇到异常)
public static void FastBlur(Bitmap SourceImage, int radius)
{
var rct = new Rectangle(0, 0, SourceImage.Width, SourceImage.Height);
var dest = new int[rct.Width * rct.Height];
var source = new int[rct.Width * rct.Height];
var bits = SourceImage.LockBits(rct, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
Marshal.Copy(bits.Scan0, source, 0, source.Length);
SourceImage.UnlockBits(bits);
if (radius < 1) return;
int w = rct.Width;
int h = rct.Height;
int wm = w - 1;
int hm = h - 1;
int wh = w * h;
int div = radius + radius + 1;
var r = new int[wh];
var g = new int[wh];
var b = new int[wh];
int rsum, gsum, bsum, x, y, i, p1, p2, yi;
var vmin = new int[max(w, h)];
var vmax = new int[max(w, h)];
var dv = new int[256 * div];
for (i = 0; i < 256 * div; i++)
{
dv[i] = (i / div);
}
int yw = yi = 0;
for (y = 0; y < h; y++)
{ // blur horizontal
rsum = gsum = bsum = 0;
for (i = -radius; i <= radius; i++)
{
int p = source[yi + min(wm, max(i, 0))];
rsum += (p & 0xff0000) >> 16;
gsum += (p & 0x00ff00) >> 8;
bsum += p & 0x0000ff;
}
for (x = 0; x < w; x++)
{
r[yi] = dv[rsum];
g[yi] = dv[gsum];
b[yi] = dv[bsum];
if (y == 0)
{
vmin[x] = min(x + radius + 1, wm);
vmax[x] = max(x - radius, 0);
}
p1 = source[yw + vmin[x]];
p2 = source[yw + vmax[x]];
rsum += ((p1 & 0xff0000) - (p2 & 0xff0000)) >> 16;
gsum += ((p1 & 0x00ff00) - (p2 & 0x00ff00)) >> 8;
bsum += (p1 & 0x0000ff) - (p2 & 0x0000ff);
yi++;
}
yw += w;
}
for (x = 0; x < w; x++)
{ // blur vertical
rsum = gsum = bsum = 0;
int yp = -radius * w;
for (i = -radius; i <= radius; i++)
{
yi = max(0, yp) + x;
rsum += r[yi];
gsum += g[yi];
bsum += b[yi];
yp += w;
}
yi = x;
for (y = 0; y < h; y++)
{
dest[yi] = (int)(0xff000000u | (uint)(dv[rsum] << 16) | (uint)(dv[gsum] << 8) | (uint)dv[bsum]); // <--- Here's where I get the exception
if (x == 0)
{
vmin[y] = min(y + radius + 1, hm) * w;
vmax[y] = max(y - radius, 0) * w;
}
p1 = x + vmin[y];
p2 = x + vmax[y];
rsum += r[p1] - r[p2];
gsum += g[p1] - g[p2];
bsum += b[p1] - b[p2];
yi += w;
}
}
// copy back to image
var bits2 = SourceImage.LockBits(rct, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
Marshal.Copy(dest, 0, bits2.Scan0, dest.Length);
SourceImage.UnlockBits(bits);
}
下面是我调用该函数的方式:
public Bitmap DownloadBlurredImage(List<string> uris, int blurradius)
{
Uri urlUri = new Uri(uris.ElementAt(0));
if (RandomSelect == true)
{
Random r = new Random();
urlUri = new Uri(uris.ElementAt(r.Next(0, uris.Count - 1)));
}
WebRequest webRequest = WebRequest.CreateDefault(urlUri);
webRequest.ContentType = "image/jpeg";
WebResponse webResponse = webRequest.GetResponse();
Stream mystream;
if ((mystream = webResponse.GetResponseStream()) != null)
{
Bitmap b = new Bitmap(mystream);
imageFilters.FastBlur(b, 10); //<--- here's where I'm calling
return b;
}
else
{
return null;
}
}
有人知道为什么会这样吗?我自己也不知道..
最佳答案
您正在使用带符号的“整数”来保存无符号数据 - 如果设置了 MSB,则会发生溢出。使用无符号整数(例如 dest
),或包围在 unchecked { }
指令中。
关于c# - System.OverflowException 在一个项目中——在另一个项目中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23449589/
当我不想要它们(或者我认为如此)时,我得到了 OverflowException 抛给我。我正在执行一些奇怪的计算,我希望这些值会溢出,从而丢弃溢出的位。看来我无法让它正常工作。基本上这是一对 i 和
有人可以解释以下行为吗: static void Main(string[] args) { checked { double
我的代码将一个字典作为参数并返回一个字典。 代码计算所有 double 值的总和,并使用总和计算每个值占总和的百分比。它返回一个新的字典,其中百分比连接到键上。 我的 Web 服务器的事件查看器中记录
我有一些用于解析网络响应的类: [DataContract] public abstract class GYResponse { [DataMember(Name = "code")]
我有一个 x64 WinForms 程序,可以与 MySQL 和 ODBC 连接器正常工作。直到一台机器让我更改为 x86,现在我有了这行代码: var someValue = data.GetStr
在向文本框输入负值时,我收到一条错误消息:Unhandled Exception: System.OverflowException: Value was either too large or to
所以这是我的代码: static void Main(string[] args) { Console.WriteLine("How many numbers are you going to
在下面最后一行的方法中,我总是遇到异常: System.OverflowException: Value was either too large or too small for an Int32.
我有一个简单的 Windows 应用程序,文本框在那里。当我在金额文本框中输入金额时,它会将其转换为另一个名为 txtrupees 的文本框中的单词。金额文本框字段最大长度设置为最后 3 位 .00
编写代码后可以归结为以下内容: var size=-1; var arr=new byte[size]; 我很惊讶它抛出一个 OverflowException . OverflowException
转换时double (或 float )到 decimal , 溢出异常是可能的。所以我写了这个小的扩展方法,通过饱和来防止这种情况: public static decimal ToDecimalS
我有一个奇怪的问题。我一直在互联网上搜索一种又好又快的高斯模糊算法。我终于找到了! 所以我所做的就是在一个新项目中尝试它 - 它工作得很好。 当我打算将该类导入到我的主项目中并尝试它时,我得到了一个
我有多个库项目,它们都依赖于支持库。我的应用程序依赖于这些多个库项目。每个库项目都在其 R.java 文件中包含支持库资源的引用。由于冗余,这会增加字段 ID 计数。 我的应用获得了 DexIndex
我在用 Decimal.Round(decimal d) MSDN 说它可以抛出 OverflowException https://msdn.microsoft.com/en-us/library/
也许是一个无用的问题: public static double Average( this IEnumerable source, Func selector ) 上述方法抛出的异常
我正在使用 NHibernate 通过 ODBC 从 Oracle 数据库中选择一个实体。 这在 32 位系统上工作正常,但在 x64 上出现以下错误:似乎行数格式不正确? (ODBC部分) Arit
我有一个包含两个值的整数数组,每个值都是 int32 的最大值: int[] factors = new int[] { 2147483647, 2147483647 }; 我正在尝试获取这两个数字的
我在 C 中有以下代码,它将根据输入的序列号生成 key 。 unsigned int32 passkey(unsigned int32 snumber) { char snstring[11
我不确定我为什么得到它...知道为什么吗? 代码: data[i] = Convert.ToByte(build, 16); 其中 build 是一个 string,其值为 0x255 例如,其他转换
这个问题在这里已经有了答案: Why dividing int.MinValue by -1 threw OverflowException in unchecked context? (3 个回答)
我是一名优秀的程序员,十分优秀!