一个项目中的System.OverflowException - 同时在另一个项目中工作

时间:2014-05-03 20:40:07

标签: c# wpf algorithm exception

我有一个奇怪的问题。我一直在互联网上搜索一个好的快速高斯模糊算法。我终于找到了一个!

所以我所做的就是在一个新项目中尝试它 - 它运作得很好。

当我打算将该类导入我的主项目然后尝试它时,我得到了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;
        }
    }

有谁知道为什么这会起作用?我自己也没有任何线索......

1 个答案:

答案 0 :(得分:1)

您使用带符号的'整数'来保存无符号数据 - 如果MSB已设置,则会发生溢出。使用无符号整数(例如dest),或环绕unchecked { }指令。