1

我正在使用该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方法时,我没有注意到这种突变。

4

0 回答 0