【问题标题】:c# external Exception in GDI+c# GDI+ 中的外部异常
【发布时间】:2010-10-26 17:03:48
【问题描述】:

我想运行这段代码

        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 wurde nicht behandelt。 Message="GDI+ 中出现一般错误。" 来源="系统.绘图" 错误代码=-2147467259 堆栈跟踪: 在 System.Drawing.Bitmap.SetPixel(Int32 x,Int32 y,颜色颜色) 在 D:\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Bild.cs:line 44 中的 Metalldetektor.Bild.ArrToPic(Int32[,] arr, Image img) 在 D:\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 58 中的 Metalldetektor.Form1.button2_Click(Object sender, EventArgs e) 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ButtonBase.WndProc(消息和 m) 在 System.Windows.Forms.Button.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 原因,Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 D:\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19 中的 Metalldetektor.Program.Main() 在 System.AppDomain._nExecuteAssembly(程序集程序集,字符串 [] 参数) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:

我不知道该怎么做,请帮忙!

【问题讨论】:

    标签: c# image exception gdi+ bitmap


    【解决方案1】:

    过去我也遇到过类似的问题,我的克隆位图有一些伪影。在研究了这个问题一段时间后,我偶然发现了this thread,它有帮助。

    尝试替换您的 Clone()

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

    用这个:

    Bitmap grayImage = new Bitmap(img);
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          我不知道这个错误,但这可能是 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很多

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-12-21
            • 2020-09-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-22
            • 2011-12-22
            相关资源
            最近更新 更多