当您想对大图像进行图像处理时,GetPixel()方法花费的时间很长,但是我认为我的算法比其他答案花费的时间更少,例如,您可以在800 * 600像素的图像上测试此代码。
Bitmap bmp = new Bitmap("SomeImage");
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[bytes];
byte[] r = new byte[bytes / 3];
byte[] g = new byte[bytes / 3];
byte[] b = new byte[bytes / 3];
// Copy the RGB values into the array.
Marshal.Copy(ptr, rgbValues, 0, bytes);
int count = 0;
int stride = bmpData.Stride;
for (int column = 0; column < bmpData.Height; column++)
{
for (int row = 0; row < bmpData.Width; row++)
{
b[count] = (byte)(rgbValues[(column * stride) + (row * 3)]);
g[count] = (byte)(rgbValues[(column * stride) + (row * 3) + 1]);
r[count++] = (byte)(rgbValues[(column * stride) + (row * 3) + 2]);
}
}
0
嗨,我有一个
bmp
加载到BMP
对象,并且im需要经过上述像素从(1,1)
像素到(100,100)
px穿过像素。使用getpixel()
方法。我使用的是ONE循环,但未成功。如果im使用多维数组的概念,应该是什么变量值?