
嗨,我有一个bmp加载到一个BMP对象和im需要通过像素作为上述图像从(1,1)像素到(100,100) px。采用getpixel()方法。我使用的是ONE循环,但没有成功。
如果我使用多维数组的概念,变量值应该是什么?
发布于 2011-05-23 07:24:36
当您想要对大型图像进行图像处理时,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]);
}
}发布于 2011-05-25 14:27:02
如果你想往右,左,右,.在一个循环中,这将执行以下操作:
for (int i = 0 ; i < bmp.Height * bmp.Width; ++i) {
int row = i / bmp.Height;
int col = i % bmp.Width;
if (row%2 != 0) col = bmp.Width - col-1;
var pixel = bmp.GetPixel(col, row);
}发布于 2011-05-16 16:35:16
您需要使用两个循环:
for (int ii = 0; ii < 100; ii++)
{
for (int jj = 0; jj < 100; jj++)
{
Color pixelColor = bitmap.GetPixel(ii, jj);
// do stuff with pixelColor
}
}https://stackoverflow.com/questions/6020406
复制相似问题