Article From:https://www.cnblogs.com/lonelyxmas/p/9688777.html
Transparent part penetration test of Png pictures

     
       private void Window_MouseMove(object sender, MouseEventArgs e)
{
NavBtnList.Clear(); Point mouseP
= e.GetPosition(this); VisualTreeHelper.HitTest(this, new HitTestFilterCallback(HitFilter), new HitTestResultCallback(HitResult), new PointHitTestParameters(mouseP)); if (NavBtnList.Count > 0) { for (int k = 0; k < NavBtnList.Count; k++) { IconBtn element = NavBtnList[k] as IconBtn; DrawingVisual drawingVisual = new DrawingVisual(); using (DrawingContext context = drawingVisual.RenderOpen()) { VisualBrush brush = new VisualBrush(element) { Stretch = Stretch.None }; context.DrawRectangle(brush, null, new Rect(0, 0, element.ActualWidth, element.ActualHeight)); context.Close(); } //dpiYou can set it yourself.// Get the DPI method: PresentationSource.FromVisual (this).CompositionTarget.TransformToDevice RenderTargetBitmap bitmap = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 96, 96, PixelFormats.Pbgra32); bitmap.Render(drawingVisual); Point p = this.TranslatePoint(mouseP, element); //Global coordinates to local coordinates point//Get the lower pixel color value of mouse. var c = getImagePointColor((bitmap as BitmapSource), (int)p.X, (int)p.Y); if (c != Convert16ToColor("#00000000")) { if (element.IsEnabled) { } break; } bitmap.Clear(); }
}
 1         //Mouse click to get all IconBtnFrame stored in NavBtnList.
 2         List<IconBtn> NavBtnList = new List<IconBtn>();
 3 
 4         public HitTestFilterBehavior HitFilter(DependencyObject dObject)
 5         {
 6             if (dObject is IconBtn)
 7             {
 8                 NavBtnList.Add(dObject as IconBtn);
 9             }
10             return HitTestFilterBehavior.Continue;
11         }
12 
13         public HitTestResultBehavior HitResult(HitTestResult result)
14         {
15             return HitTestResultBehavior.Continue;
16         }

 

Leave a Reply

Your email address will not be published. Required fields are marked *