tags:

views:

71

answers:

2

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?

A: 

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.

Foxfire
That did the same thing as my method above. Either allocate for a bitmap source, or allocate for a tiny 1x1 cropped bitmap.The actual issue why this wasn't working (the exception) had to do with the mouse position itself.
csciguy
A: 

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.

csciguy