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?
Why don't you use CopyPixels directly on the BitmapSource?
http://msdn.microsoft.com/en-us/library/ms616042(v=VS.100).aspx
Moreover are you sure your stride is really 4? Stride does include padding.
The method I stated worked. The problem laid within the actual position of the mouse cursor itself. The position that was being returned in my method was relative to the main window. That needed to be converted to the main canvas. Once that was done, the posted code worked fine. I could check the pixel[3] value to determine my alpha channel color.
精彩评论