[英]Exception while Marshal.Copy()
為了更快地訪問,我想使用字節數組讀取位圖的像素。 因此,我使用了此處提供的代碼: https : //msdn.microsoft.com/de-de/library/system.drawing.imaging.imagelockmode(v=vs.110).aspx
這是我的代碼:
private static int GetBitmapDataInByteArray(out byte[] bArr, ref System.Drawing.Bitmap bmp)
{
int Length = -1;
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(
new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
Length = System.Math.Abs(bmpData.Stride) * bmpData.Height;
bArr = new byte[Length];
/* Exception on this line: */
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, bArr, 0, Length);
bmp.UnlockBits(bmpData);
return Length;
}
我得到了System.AccessViolationException-在受保護的內存中讀取或寫入。
有人知道為什么嗎?
Philippe Pare是對的。 手動指定pixelformat可解決此問題。 也許是32/64位之間的沖突?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.