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 is the exception information:

System.Runtime.InteropServices.ExternalException wurde nicht behandelt. Message = "A general error occurred in GDI +." Source = "System.Drawing" ErrorCode = -2147467259 Stack traces: in System.Drawing.Bitmap.SetPixel (Int32 x, Int32 y, Color color) in Metalldetektor.Bild.ArrToPic (Int32 [,] arr, Image img) in D : \ Documents \ Visual Studio 2008 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Bild.cs: line 44 in Metalldetektor.Form1.button2_Click (object sender, EventArgs e) in D: \ Documents \ Visual Studio 2008 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1 .cs: ​​line 58 in System.Windows.Forms.Control.OnClick (EventArgs e) in System.Windows.Forms.Button.OnMouseUp (MouseEventArgs mevent) in System.Windows.Forms.Control.WmMouseUp (Message & m, MouseButtons button ,clicks Int32) in System.Windows.Forms.Control.WndProc (Message & m) in System.Windows.Forms.ButtonBase.WndProc (Message & m) in System.Windows.Forms.Button.WndProc (Message & m) in System .Windows.Forms.Control.ControlNativeWindow.WndProc (Message & m) in System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) in System.Windows.Forms.UnsafeNativeMethod.Disp & msg) in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (Int32 dwComponentID, reason Int32, Int32 pvLoopData) in System.Windows.Forms.ApplicationRop.Thread , ApplicationContext context) in System.Windows.Forms.Application.ThreadContext.RunMessageLoop (reason Int32, ApplicationContext context) in Metalldetektor.Program.Main () in D: \ Documents \ Visual Studio 2008 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Program.cs: line 19 in System.AppDomain._nExecuteAssembly (assembly assembly, String [ ] args) in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly () in System.Threading.ExecutionContext.Run (ExecutionContext executeContext, ContextCallback callback, object state) in System.Threading.ThreadHelper.ThreadStart () InnerException:HostProc.RunUsersAssembly () in System.Threading.ExecutionContext.Run (ExecutionContext executeContext, ContextCallback callback, object state) in System.Threading.ThreadHelper.ThreadStart () InnerException:HostProc.RunUsersAssembly () in System.Threading.ExecutionContext.Run (ExecutionContext executeContext, ContextCallback callback, object state) in System.Threading.ThreadHelper.ThreadStart () InnerException:

, , , !

+3
4

, . , , .

Clone()

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

:

Bitmap grayImage = new Bitmap(img);
+2

, 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-) teammate. . , .

0
source

Why are you cloning another image if you just overwrite everything (or the top left rectangle)?

0
source

Source: https://habr.com/ru/post/1708479/


All Articles