WPF: Detect Image click only on non-transparent portion -


i have image control in wpf contains image lots of transparent pixels. right now, mousedown event on image fires whenever click within full rectangular region of image control. way detect if mouse click occurred on nontransparent portion of image.

what best way of doing this?

using technique in this answer can derive image create opaqueclickableimage responds hit-testing in sufficiently non-transparent areas of image:

public class opaqueclickableimage : image {     protected override hittestresult hittestcore(pointhittestparameters hittestparameters)     {         var source = (bitmapsource)source;          // pixel of source hit         var x = (int)(hittestparameters.hitpoint.x / actualwidth * source.pixelwidth);         var y = (int)(hittestparameters.hitpoint.y / actualheight * source.pixelheight);          // copy single pixel new byte array representing rgba         var pixel = new byte[4];         source.copypixels(new int32rect(x, y, 1, 1), pixel, 4, 0);          // check alpha (transparency) of pixel         // - threshold can adjusted 0 255         if (pixel[3] < 10)             return null;          return new pointhittestresult(this, hittestparameters.hitpoint);     } } 

after adding class, use regular image:

<utils:opaqueclickableimage name="image" source="http://entropymine.com/jason/testbed/pngtrans/rgb8_t_bk.png" stretch="none"/> 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -