首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过BMP中的像素

通过BMP中的像素
EN

Stack Overflow用户
提问于 2011-05-16 16:30:21
回答 7查看 29.1K关注 0票数 29

嗨,我有一个bmp加载到一个BMP对象和im需要通过像素作为上述图像从(1,1)像素到(100,100) px。采用getpixel()方法。我使用的是ONE循环,但没有成功。

如果我使用多维数组的概念,变量值应该是什么?

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2011-05-23 07:24:36

当您想要对大型图像进行图像处理时,GetPixel()方法需要很长的时间,但是我认为我的算法比其他答案花费的时间更少,例如,您可以在800 * 600像素的图像上测试这段代码。

代码语言:javascript
复制
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]);
    }
}
票数 56
EN

Stack Overflow用户

发布于 2011-05-25 14:27:02

如果你想往右,左,右,.在一个循环中,这将执行以下操作:

代码语言:javascript
复制
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);
}
票数 8
EN

Stack Overflow用户

发布于 2011-05-16 16:35:16

您需要使用两个循环:

代码语言:javascript
复制
for (int ii = 0; ii < 100; ii++)
{
  for (int jj = 0; jj < 100; jj++)
  {
    Color pixelColor = bitmap.GetPixel(ii, jj);
    // do stuff with pixelColor
  }
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6020406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档