我正在使用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[]
数组时,存储的信息是正确的,所以我不知道这里发生了什么。
答案 0 :(得分:5)
这些价值观完全正常。它们是32bpp像素,alpha通道位于0xff,这是通常的值。换句话说:-8682109 == 0xff7b8583。任何alpha值> = 128将使值为负,因为它设置符号位。
使用uint []而不是int []可以更容易看到。
答案 1 :(得分:0)
恕我直言,你的字节数是一个字节数,现在你将字节除以4并期望一组整数,实际上是字节。演员表不是你想要的。
要将字节数组转换为int数组,请使用:
System.BitConverter.ToInt32(mybyteArray,4);