我正在尝试使用 Bitmap 和 GetPixels 获取图像中的所有像素。现在我知道它的效率非常低,所以我一直在研究 LockBits。我已经成功地制作了我认为锁定位的东西,但我无法获得每一个像素。到目前为止,我的代码是...
//Creates Rectangle for holding picture
Rectangle bmpRec = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(bmpRec, ImageLockMode.ReadWrite, bmp.PixelFormat);
IntPtr Pointer = bmpData.Scan0; //Scans the first line of data
int DataBytes = Math.Abs(bmpData.Stride) * bmp.Height; //Gets array size
byte[] rgbValues = new byte[DataBytes]; //Creates array
string Pix = " ";
Marshal.Copy(Pointer, rgbValues, 0, DataBytes); //Copies of out memory
bmp.UnlockBits(bmpData);
for (int p = 0; p < DataBytes; p++)
{
Pix += " " + rgbValues[p];
}
我想使用 Lockbits,因为它是获取像素的最佳方式。有什么帮助吗?
谢谢你。