How to determine pixel color of System.Windows.Controls.Image?
- by csciguy
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?