Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!


1 Answers

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);
like image 196
Igal Tabachnik Avatar answered Dec 03 '25 18:12

Igal Tabachnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!