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

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

问题描述

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

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

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

我要查询以颜色是在特定点什么。例如,什么是像素的RGB值(65,32)?

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

虽然我会承认有一点猴子看,猴子做打算跟code。
无论如何,有没有处理此字节数组转换为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?

推荐答案

所得字节阵列的除pretation取决于源位图的象素格式,但在32位,ARGB的最简单的情况图像,每个像素将被包括的字节数组中的四个字节。第一像素是正是如此PTED间$ P $:

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