2009-05-16 11 views
3

ich dieses Stück CodeC# externe Ausnahme in GDI +

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

ausgeführt werden soll, wenn ich den Code ausführen ich eine Ausnahme in dieser Zeile: grayImage.SetPixel (x, y, grau);

Hier sind die Ausnahmedetails:

System.Runtime.InteropServices.ExternalException Wurde nicht Behandelt. Message = "Ein allgemeiner Fehler ist in GDI + aufgetreten." Source = "System.Drawing" Error-Code = -2147467259 Stacktrace: bei System.Drawing.Bitmap.SetPixel (Int32 x, y Int32, Farbe Farbe) bei Metalldetektor.Bild.ArrToPic (Int32 [,] arr, Bild img) in D: \ Dokumente \ Visual Studio 2008 \ Projekte \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Bild.cs: Zeile 44 bei Metalldetektor.Form1.button2_Click (Objekt Absender, EventArgs e) in D: \ Dokumente \ Visual Studio 2008 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs: Zeile 58 bei System.Windows.Forms.Control.OnClick (EventArgs e) bei System.Windows.Forms.Button.OnMouseUp (MouseEventArgs mevent) bei System.Windows.Forms.Control. WmMouseUp (Nachricht & m, Maustasten, Int32 Klicks) bei System.Windows.Forms.Control.WndProc (Message & m) bei System.Windows.Forms.ButtonBase.WndProc (Message & m) bei System.Windows.Forms.Button.WndProc (Message & m) an System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message & m) bei System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) bei System.Windows.Forms.UnsafeNativeMethods. DispatchMessageW (MSG & msg) bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (Int32 DwComponentID, Grund Int32, Int32 pvLoopData) bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (Int32 Grund, ApplicationContext Kontext) bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop (Int32 Grund, ApplicationContext Kontext) bei Metalldetektor.Program.Main() in D: \ Dokumente \ Visual Studio 2008 \ Projekte \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Program.cs: Zeile 19 bei System.AppDomain._nExecuteAssembly (Assembly Assembly, String [] Args) bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() bei System.Threading.ExecutionContext.Run (ExecutionContext ExecutionContext, Context Rückruf, Objektzustand) bei System.Threading.ThreadHelper.ThreadStart() Innerexception:

Ich weiß nicht, was ich tun soll, bitte helfen Sie mir!

Antwort

2

Ich hatte ein ähnliches Problem in der Vergangenheit, wo meine geklonte Bitmap einige Artefakte hatte. Nachdem ich dieses Problem für eine Weile erforscht hatte, stolperte ich über this thread, was half.

Versuchen Sie Clone()

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

mit diesem ersetzen:

Bitmap grayImage = new Bitmap(img); 
+0

Dank funktioniert es jetzt gut –

0

Ich weiß nicht über den Fehler, aber das könnte ein Fall für LockBits sein ... Ich werde sehen, ob ich ein Beispiel zaubern kann.

Hier ist ein vereinfachtes Beispiel eine Reihe von Daten an einen ARGB Bitmap zu schreiben:

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

Dieser Ansatz sollte ein viel schneller als SetPixel.

0

Vor langer Zeit gab es einen Fehler bei der Verarbeitung von Bildern (Erstellen von Bildern durch Lesen von Binärdaten vom SQL-Server) auf einem System meines Teamkollegen.Derselbe Code funktionierte auf anderen Maschinen einwandfrei. Es stellte sich heraus, dass er ein Update für den Grafiktreiber installiert hatte und das Probleme verursachte.

0

Warum klonen Sie ein anderes Bild, wenn Sie nur alles (oder das obere linke Rechteck) überschreiben?