在 BMP 中穿越像素 [英] Travel through pixels in BMP

查看:20
本文介绍了在 BMP 中穿越像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 bmp 加载到 BMP 对象,我需要像上图来自 (1,1) 像素到 (100,100) px .使用 getpixel() 方法.我使用的是 ONE 循环,但没有成功.

Hi i have a bmp loaded to a BMP object and im required to travel though the pixels as the above image from (1,1) pixel to (100,100) px . using getpixel() method. I was using was ONE loop but it was not successful .

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

If im using the concept of multidimensional array what should be variable values ?

推荐答案

当你想做图像处理时在巨大的图像上 GetPixel() 方法需要很长时间但我认为我的算法需要更少时间比其他答案,例如您可以在 800 * 600 上测试此代码像素图像.

When you want to doing image processing on huge images GetPixel() method takes long time but I think my algorithm takes less time than other answers , for example you can test this code on 800 * 600 pixels image.

<小时>

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]);
    }
}

这篇关于在 BMP 中穿越像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆