[英]Bitmap to int[] using Marshal.Copy()
我正在使用Marshal.Copy()
將像素信息從Bitmap
復制到int[]
數組,問題在於這樣一個事實,即將傳入該數組的信息全部出錯,例如:
[0] = -8682109;
[1] = -8682109;
[2] = -8616573;
[3] = -8616573;
[4] = -8550527;
and so on...
該方法的代碼是:
private unsafe int[] BmpToBytes_Unsafe(Bitmap bmp)
{
BitmapData bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size),
ImageLockMode.ReadOnly,
PixelFormat.Format32bppRgb);
// number of bytes in the bitmap
byteCount = bData.Stride * (bmp.Height);
int[] bytes = new int[byteCount / 4];
Marshal.Copy(bData.Scan0, bytes, 0, byteCount/4);
// don't forget to unlock the bitmap!!
bmp.UnlockBits(bData);
return bytes;
當我使用byte[]
數組時,存儲的信息是正確的,因此我不知道這里發生了什么。
這些值是完全正常的。 它們是32bpp像素,alpha通道為通常值0xff。 換句話說:-8682109 == 0xff7b8583。 任何大於等於128的alpha值都會使該值變為負值,因為它會設置符號位。
使用uint []而不是int []可以使它更容易看到。
恕我直言,您的字節數是字節的個數,現在您將字節除以4,並期待一組整數,它們實際上是字節。 投射不按照您的要求執行。
要將字節數組轉換為int數組,請使用:
System.BitConverter.ToInt32(mybyteArray,4);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.