Tutorial :c# external Exception in GDI+


I want to run this piece of code

        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);              }          }  

if I run the code I get an exception in this line: grayImage.SetPixel(x, y, grau);

Here are the Exception Details:

System.Runtime.InteropServices.ExternalException wurde nicht behandelt. Message="A generic error occurred in 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:

I don't know what to do so please help!


I had a similar problem in the past where my cloned bitmap had some artifacts. After researching this problem for a while, I stumbled upon this thread which helped.

Try replacing your Clone()

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

with this:

Bitmap grayImage = new Bitmap(img);  


A long time ago there was some error when processing image (creating image by reading binary data from SQL server) on one of my teammate's system. The same code worked fine on other machines. It turned out that he had installed some update for graphics driver and that caused problems.


Why are you cloning another image if you're just overwriting everything (or the top-left rectangle)?


I don't know about the bug, but this might be a case for LockBits... I'll see if I can whip up an example.

Here's a simplified example writing an array of data to an ARGB bitmap:

    // 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  

This approach should be a lot faster than SetPixel.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »