Ich extrahiere Reihen von Pixeln aus einem Bild in ein Array von int mit Marshal.Copy. Alles funktioniert gut, bis ich die letzte Zeile des Bildes erreiche. Wenn ich versuche, es zu extrahieren, erhalte ich eine nette Ausnahme:System.AccessViolationException beim Versuch, die letzte Zeile eines Bitmap-Bilds zu kopieren
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Hier ist ein kleiner Testfall, der den Fehler reproduziert. Was mache ich falsch ?
[Test]
public void testMarshalCopy() {
Bitmap image = new Bitmap("../../TestResources/barcode.jpg");
int left = 0;
int top = image.Height - 1;
int width = image.Width;
int height = 1;
Rectangle zone = new Rectangle(left, top, width, height);
BitmapData data = image.LockBits(zone, System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);
IntPtr pointer = data.Scan0;
int[] pixels = new int[width * height];
Marshal.Copy(pointer, pixels, 0, pixels.Length); // throws System.AccessViolationException
image.UnlockBits(data);
}
Was ist Ihr Bildpixelformat? Ist jedes Pixel == 'sizeof (int)'? – heavyd
Fwiw, das ist die erste Zeile. Bitmaps werden auf dem Kopf stehend gespeichert. –
@Hans: Danke für die Information! – Wookai