旅行通过BMP像素 [英] Travel through pixels in BMP

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

问题描述

您好我有一个 BMP 加载到旅游虽然像素作为上述要求的一个 BMP 对象和IM图像从(1,1)像素(100,100)像素。使用与getPixel()方法。我用了一个循环,但没有成功。

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

解决方案
  

当你想要做的图像处理   在巨大的图像getPixel()方法需要很长时间   但我认为我的算法需要更少   时间比其他的答案,比如   您可以测试在800 * 600这个code   像素的图像。


  BMP位图=新位图(SomeImage);

//锁定位的位。
矩形RECT =新的Rectangle(0,0,bmp.Width,bmp.Height);
的BitmapData bmpData = bmp.LockBits(矩形,ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);

//获取第一行的地址。
IntPtr的PTR = bmpData.Scan0;

//声明一个数组来保存位图的字节。
INT字节= bmpData.Stride * bmp.Height;
byte []的rgbValues​​ =新的字节[字节];
byte []的R =新的字节[字节/ 3]。
byte []的G =新的字节[字节/ 3]。
byte []的B =新的字节[字节/ 3]。

//复制的RGB值到数组。
Marshal.Copy(PTR,rgbValues​​,0,字节);

诠释计数= 0;
INT跨距= bmpData.Stride;

对于(INT列= 0;柱< bmpData.Height;列++)
{
    对于(INT行= 0;行< bmpData.Width;排++)
    {
        B〔数=(字节)(rgbValues​​ [(列*步幅)+(行* 3)]);
        政[统计] =(字节)(rgbValues​​ [(列*步幅)+(行* 3)+ 1]);
        ř[计数++] =(字节)(rgbValues​​ [(柱*步幅)+(行* 3)+2]);
    }
}
 

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 ?

解决方案

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天全站免登陆