Lockbits:数组序列在某处发生变异

时间:2016-09-03 12:31:33

标签: c# bitmap lockbits

我正在使用lockbit方法来处理图片,但我注意到在使用save类型的Bitmap方法保存图片后,我的数据在字节数组中操作我从锁定位出来,被操纵。

例如假设以下方法:

 Bitmap bmp = new Bitmap(fs);
 BitmapData bits = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
 IntPtr ptr = bits.Scan0;
 int arraySize = Math.Abs(bits.Stride) * bmp.Height;
 Byte[] rgbValues = new Byte[arraySize];
 System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, arraySize);

当我尝试通过使用例如:

将数组中的所有值设置为0来测试它时
 for(int x = 0; x < arraySize; x++){
     rgbValues[x] = (Byte) (rgbValues[x] % 2 == 0 ? 0 : 1);
  }
 System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, arraySize);
 bmp.UnlockBits(bits);
 bmp.Save("somewhere");

现在,当我使用相同的技术回读保存的图像时,我会在锁定位中看到所获得的字节数组中的值为10,20或其他奇怪值。

我不认为这可能是正常行为,因为当使用较慢的GetPixel方法时,我没有注意到这种突变。

0 个答案:

没有答案
相关问题