Finding specific pixel colors of a BitmapImage

Posted by Andrew Shepherd on Stack Overflow See other posts from Stack Overflow or by Andrew Shepherd
Published on 2009-07-24T10:49:35Z Indexed on 2010/04/17 7:03 UTC
Read the original article Hit count: 497

Filed under:
|
|

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

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

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

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?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about bitmap