How to determine pixel color of System.Windows.Controls.Image?

Posted by csciguy on Stack Overflow See other posts from Stack Overflow or by csciguy
Published on 2010-04-26T18:39:08Z Indexed on 2010/04/26 18:43 UTC
Read the original article Hit count: 278

Filed under:
|
|

I have an Image from (System.Windows.Controls.Image).

This image is positioned on a main canvas.

I want to determine the alpha channel value of the mouse cursor when I click on any portion of this image.

When doing something like the following, I'm getting an exception. {"Value does not fall within the expected range."} System.Exception {System.ArgumentException}

Code:

try{
    CroppedBitmap cb = new CroppedBitmap(ac.displayImage.Source as BitmapSource,
                new Int32Rect((int)mousePoint.X,
                    (int)mousePoint.Y, 1, 1));                          

            byte[] pixels = new byte[4];


               cb.CopyPixels(pixels, 4, 0);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

The mousePoint.X, and mousePoint.Y are obtained when the user clicks on the main window. Is there a better way to do this?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about .NET