WPF cursor on partially transparent image

I have a png that contains transparent areas and I set it to an image tag, but how can I position the cursor on my hand when it is above the opaque part of the image?

Thank you, Tony

+7
transparency wpf cursor
source share
1 answer

To do this, you will need to look at the bitmap. The WPF testing engine considers that any pixel drawn with a transparent brush is still clickable, although invisible. This is usually good, but it interferes with what you are trying to do. Since .png is a paint with a transparent brush, all .png is considered painted when performing impact testing.

What you need to do in the MouseMove handler:

  • Go ahead and call the hit check as usual.
  • For each HitTestResult you will return, check if it is Image , and if so, then a transparent pixel is under the mouse
  • When you get hit on a non-image or opaque pixel image, stop.
  • Determine the value of Cursor depending on what the mouse is over.

To determine if the mouse is above a transparent pixel in an image:

  • Get mouse position relative to image ( e.GetPosition(image) )
  • If you use stretch, you should next perform a stretch calculation to get the index of the bitmap
  • Use BitmapSource.CopyPixels to copy a 1-pixel rectangle into an array (i.e. only one pixel of the mouse has ended)
  • Check the resulting pixel value to see if it is a transparent pixel
+4
source share

All Articles