@@ -14,7 +14,7 @@ public class OpaqueClickableImage : Image
1414 {
1515 protected override HitTestResult HitTestCore ( PointHitTestParameters hitTestParameters )
1616 {
17- var source = ( BitmapSource ) Source ;
17+ var source = new FormatConvertedBitmap ( ( BitmapSource ) Source , PixelFormats . Bgra32 , null , 0 ) ;
1818
1919 // Get the pixel of the source that was hit
2020 var x = ( int ) ( hitTestParameters . HitPoint . X / ActualWidth * source . PixelWidth ) ;
@@ -23,69 +23,15 @@ protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParam
2323 if ( x == source . PixelWidth ) x -- ;
2424 if ( y == source . PixelHeight ) y -- ;
2525
26- var pixel = GetPixels ( source ) [ x , y ] ;
26+ var pixelxy = new Int32Rect ( x , y , 1 , 1 ) ;
27+ var pixelbgra = new byte [ 4 ] ;
2728
28- if ( pixel . Alpha < 5 )
29+ source . CopyPixels ( pixelxy , pixelbgra , source . PixelWidth * 4 , 0 ) ;
30+
31+ if ( pixelbgra [ 3 ] < 5 )
2932 return null ;
3033
3134 return new PointHitTestResult ( this , hitTestParameters . HitPoint ) ;
3235 }
33-
34- public PixelColor [ , ] GetPixels ( BitmapSource source )
35- {
36- if ( source . Format != PixelFormats . Bgra32 )
37- source = new FormatConvertedBitmap ( source , PixelFormats . Bgra32 , null , 0 ) ;
38-
39- int width = source . PixelWidth ;
40- int height = source . PixelHeight ;
41- PixelColor [ , ] result = new PixelColor [ width , height ] ;
42-
43- BitmapSourceHelper . CopyPixels ( source , result , width * 4 , 0 ) ;
44- //source.CopyPixels(result, width * 4, 0);
45- return result ;
46- }
47- }
48-
49- [ StructLayout ( LayoutKind . Sequential ) ]
50- public struct PixelColor
51- {
52- public byte Blue ;
53- public byte Green ;
54- public byte Red ;
55- public byte Alpha ;
56- }
57-
58- public static class BitmapSourceHelper
59- {
60- #if UNSAFE
61- public unsafe static void CopyPixels ( this BitmapSource source , PixelColor [ , ] pixels , int stride , int offset )
62- {
63- fixed( PixelColor * buffer = & pixels [ 0 , 0 ] )
64- source . CopyPixels (
65- new Int32Rect ( 0 , 0 , source . PixelWidth , source . PixelHeight ) ,
66- ( IntPtr ) ( buffer + offset ) ,
67- pixels . GetLength ( 0 ) * pixels . GetLength ( 1 ) * sizeof ( PixelColor ) ,
68- stride ) ;
69- }
70- #else
71- public static void CopyPixels ( this BitmapSource source , PixelColor [ , ] pixels , int stride , int offset )
72- {
73- var height = source . PixelHeight ;
74- var width = source . PixelWidth ;
75- var pixelBytes = new byte [ height * width * 4 ] ;
76- source . CopyPixels ( pixelBytes , stride , 0 ) ;
77- int y0 = offset / width ;
78- int x0 = offset - width * y0 ;
79- for ( int y = 0 ; y < height ; y ++ )
80- for ( int x = 0 ; x < width ; x ++ )
81- pixels [ x + x0 , y + y0 ] = new PixelColor
82- {
83- Blue = pixelBytes [ ( y * width + x ) * 4 + 0 ] ,
84- Green = pixelBytes [ ( y * width + x ) * 4 + 1 ] ,
85- Red = pixelBytes [ ( y * width + x ) * 4 + 2 ] ,
86- Alpha = pixelBytes [ ( y * width + x ) * 4 + 3 ] ,
87- } ;
88- }
89- #endif
9036 }
9137}
0 commit comments