C#中GDI+发生外部异常

3

我想运行这段代码

        Bitmap grayImage = (Bitmap)img.Clone();

        for (int x = 0; x < arr.GetLength(0); x++)
        {
            for (int y = 0; y < arr.GetLength(1); y++)
            {
                int col = arr[x, y];
                Color grau = Color.FromArgb(col, col, col);
                grayImage.SetPixel(x, y, grau);
            }
        }

如果我运行代码,就会在这一行中出现异常:grayImage.SetPixel(x, y, grau);

以下是异常详细信息:

System.Runtime.InteropServices.ExternalException 未被处理。 Message="GDI+ 中发生一般性错误。" Source="System.Drawing" ErrorCode=-2147467259 StackTrace: at System.Drawing.Bitmap.SetPixel(Int32 x, Int32 y, Color color) at Metalldetektor.Bild.ArrToPic(Int32[,] arr, Image img) in D:\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Bild.cs:line 44 at Metalldetektor.Form1.button2_Click(Object sender, EventArgs e) in D:\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 58 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at Metalldetektor.Program.Main() in D:\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

我不知道该怎么办,所以请帮忙!

4个回答

2

我曾经遇到过类似的问题,我的克隆位图出现了一些伪像。经过一段时间的研究,我偶然发现了这个帖子,对我很有帮助。

尝试替换你的Clone()方法。

Bitmap grayImage = (Bitmap)img.Clone();

使用这个:

Bitmap grayImage = new Bitmap(img);

0

我不知道这个错误,但这可能是使用LockBits的情况...我会看看能否提供一个示例。

这里有一个简化的示例,将数据数组写入ARGB位图:

    // fake data
    int[,] data = new int[100, 150];
    int width = data.GetLength(0), height= data.GetLength(1);
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++)
            data[x, y] = x + y;

    // process it into a Bitmap
    Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
    BitmapData bd = bmp.LockBits(new Rectangle(0, 0, width, height),
       ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
    unsafe {
        byte* root = (byte*)bd.Scan0;
        for (int y = 0; y < height; y++) {
            byte* pixel = root;
            for (int x = 0; x < width; x++) {
                byte col = (byte)data[x, y];
                pixel[0] = col;
                pixel[1] = col;
                pixel[2] = col;
                pixel[3] = 255;
                pixel += 4;
            }
            root += bd.Stride;
        }
    }
    bmp.UnlockBits(bd);
    bmp.Save("foo.bmp"); // or show on screen, etc

这种方法比 SetPixel 快得多。


0
很久以前,在我的一个队友的系统上处理图像时出现了一些错误(通过从SQL服务器读取二进制数据创建图像)。相同的代码在其他机器上运行良好。后来发现他安装了一些图形驱动程序更新,导致了问题。

0

如果您只是要覆盖整个图像(或左上角的矩形),为什么还要克隆另一张图像呢?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接