查找 BitmapImage 的特定像素颜色 [英] Finding specific pixel colors of a BitmapImage

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

问题描述

我有一个从 .JPG 文件加载的 WPF BitmapImage,如下所示:

I have a WPF BitmapImage which I loaded from a .JPG file, as follows:

this.m_image1.Source = new BitmapImage(new Uri(path));

我想查询特定点的颜色.例如,像素 (65,32) 处的 RGB 值是多少?

I want to query as to what the colour is at specific points. For example, what is the RGB value at pixel (65,32)?

我该怎么做?我正在采取这种方法:

How do I go about this? I was taking this approach:

ImageSource ims = m_image1.Source;
BitmapImage bitmapImage = (BitmapImage)ims;
int height = bitmapImage.PixelHeight;
int width = bitmapImage.PixelWidth;
int nStride = (bitmapImage.PixelWidth * bitmapImage.Format.BitsPerPixel + 7) / 8;
byte[] pixelByteArray = new byte[bitmapImage.PixelHeight * nStride];
bitmapImage.CopyPixels(pixelByteArray, nStride, 0);

虽然我承认有一点猴子看,猴子确实继续使用这段代码.无论如何,有没有一种直接的方法来处理这个字节数组以转换为 RGB 值?

Though I will confess there's a bit of monkey-see, monkey do going on with this code. Anyway, is there a straightforward way to process this array of bytes to convert to RGB values?

推荐答案

对生成的字节数组的解释取决于源位图的像素格式,但在最简单的 32 位 ARGB 图像的情况下,每个像素将由字节数组中的四个字节组成.第一个像素将被解释为:

The interpretation of the resulting byte array is dependent upon the pixel format of the source bitmap, but in the simplest case of a 32 bit, ARGB image, each pixel will be composed of four bytes in the byte array. The first pixel would be interpreted thusly:

alpha = pixelByteArray[0];
red   = pixelByteArray[1];
green = pixelByteArray[2];
blue  = pixelByteArray[3];

要处理图像中的每个像素,您可能需要创建嵌套循环来遍历行和列,通过每个像素中的字节数递增索引变量.

To process each pixel in the image, you would probably want to create nested loops to walk the rows and the columns, incrementing an index variable by the number of bytes in each pixel.

某些位图类型将多个像素合并为一个字节.例如,单色图像每个字节包含八个像素.如果您需要处理每像素 24/32 位以外的图像(简单的图像),那么我建议您找一本涵盖位图底层二进制结构的好书.

Some bitmap types combine multiple pixels into a single byte. For instance, a monochrome image packs eight pixels into each byte. If you need to deal with images other than 24/32 bit per pixels (the simple ones), then I would suggest finding a good book that covers the underlying binary structure of bitmaps.

这篇关于查找 BitmapImage 的特定像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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