@@ -97,6 +97,12 @@ public static implicit operator Point(POINT point)
9797 // modes
9898 BlendMode blendMode ;
9999
100+ // clear buffers
101+ Int32Rect emptyRect ;
102+ int bytesPerPixel ;
103+ byte [ ] emptyPixels ;
104+ int emptyStride ;
105+
100106 private ToolMode _currentTool = ToolMode . Draw ;
101107 public ToolMode CurrentTool
102108 {
@@ -128,7 +134,6 @@ void Start()
128134 RenderOptions . SetBitmapScalingMode ( gridImage , BitmapScalingMode . NearestNeighbor ) ;
129135 RenderOptions . SetEdgeMode ( gridImage , EdgeMode . Aliased ) ;
130136 w = ( MainWindow ) Application . Current . MainWindow ;
131- //var gridScaleX = (int)gridImage.Width / canvasResolutionX;
132137 gridBitmap = new WriteableBitmap ( canvasResolutionX , canvasResolutionY , dpiX , dpiY , PixelFormats . Bgra32 , null ) ;
133138 gridImage . Source = gridBitmap ;
134139 DrawBackgroundGrid ( ) ;
@@ -138,7 +143,6 @@ void Start()
138143 RenderOptions . SetBitmapScalingMode ( outlineImage , BitmapScalingMode . NearestNeighbor ) ;
139144 RenderOptions . SetEdgeMode ( outlineImage , EdgeMode . Aliased ) ;
140145 w = ( MainWindow ) Application . Current . MainWindow ;
141- //var gridScaleX = (int)gridImage.Width / canvasResolutionX;
142146 outlineBitmap = new WriteableBitmap ( canvasResolutionX , canvasResolutionY , dpiX , dpiY , PixelFormats . Bgra32 , null ) ;
143147 outlineImage . Source = outlineBitmap ;
144148
@@ -151,6 +155,12 @@ void Start()
151155 canvasBitmap = new WriteableBitmap ( canvasResolutionX , canvasResolutionY , dpiX , dpiY , PixelFormats . Bgra32 , null ) ;
152156 drawingImage . Source = canvasBitmap ;
153157
158+ // init clear buffers
159+ emptyRect = new Int32Rect ( 0 , 0 , canvasBitmap . PixelWidth , canvasBitmap . PixelHeight ) ;
160+ bytesPerPixel = canvasBitmap . Format . BitsPerPixel / 8 ;
161+ emptyPixels = new byte [ emptyRect . Width * emptyRect . Height * bytesPerPixel ] ;
162+ emptyStride = emptyRect . Width * bytesPerPixel ;
163+
154164 // setup preview area
155165 RenderOptions . SetBitmapScalingMode ( imgPreview1x , BitmapScalingMode . NearestNeighbor ) ;
156166 imgPreview1x . Source = canvasBitmap ;
@@ -663,13 +673,13 @@ void drawingMouseWheel(object sender, MouseWheelEventArgs e)
663673 private void OnClearButton ( object sender , RoutedEventArgs e )
664674 {
665675 ClearImage ( canvasBitmap ) ;
676+ UpdateOutline ( ) ;
666677 }
667678
668679 // clears bitmap by re-creating it
669- void ClearImage ( WriteableBitmap target )
680+ void ClearImage ( WriteableBitmap targetBitmap )
670681 {
671- canvasBitmap = new WriteableBitmap ( canvasResolutionX , canvasResolutionY , dpiX , dpiY , PixelFormats . Bgra32 , null ) ;
672- drawingImage . Source = canvasBitmap ;
682+ targetBitmap . WritePixels ( emptyRect , emptyPixels , emptyStride , 0 ) ;
673683 }
674684
675685 private void OnSaveButton ( object sender , RoutedEventArgs e )
@@ -747,7 +757,7 @@ void OnKeyDown(object sender, KeyEventArgs e)
747757 c2 . Blue = c1 . B ;
748758 currentColor = c2 ;
749759 rectCurrentColor . Fill = new SolidColorBrush ( Color . FromArgb ( c2 . Alpha , c2 . Red , c2 . Green , c2 . Blue ) ) ;
750- // Console.WriteLine(cursor.X + "," + cursor.Y + " = " + c1);
760+ // Console.WriteLine(cursor.X + "," + cursor.Y + " = " + c1);
751761 break ;
752762 case Key . X : // swap current/secondary colors
753763 var tempcolor = rectCurrentColor . Fill ;
@@ -1058,6 +1068,63 @@ public static Point GetCursorPosition()
10581068 return lpPoint ;
10591069 }
10601070
1071+ private void chkOutline_Click ( object sender , RoutedEventArgs e )
1072+ {
1073+ if ( chkOutline . IsChecked == true )
1074+ {
1075+ UpdateOutline ( ) ;
1076+ }
1077+ else // clear
1078+ {
1079+ ClearImage ( outlineBitmap ) ;
1080+ }
1081+ }
1082+
1083+ private void rectHueBar_MouseDown ( object sender , MouseButtonEventArgs e )
1084+ {
1085+ POINT cursor ;
1086+ GetCursorPos ( out cursor ) ;
1087+ var c = Win32GetScreenPixel ( ( int ) cursor . X , ( int ) cursor . Y ) ;
1088+ //Console.WriteLine("color:"+c);
1089+ var f = rectSaturation . Fill ;
1090+
1091+ // build hue gradient
1092+ LinearGradientBrush myBrush = new LinearGradientBrush ( ) ;
1093+ var c1 = new Color ( ) ;
1094+ c1 . R = 255 ;
1095+ c1 . G = 255 ;
1096+ c1 . B = 255 ;
1097+ c1 . A = 255 ;
1098+ var c2 = new Color ( ) ;
1099+ c2 . R = c . R ;
1100+ c2 . G = c . G ;
1101+ c2 . B = c . B ;
1102+ c2 . A = 255 ;
1103+ myBrush . StartPoint = new Point ( 0 , 0 ) ;
1104+ myBrush . EndPoint = new Point ( 1 , 0 ) ;
1105+
1106+ var g1 = new GradientStop ( c1 , 0.0 ) ;
1107+ myBrush . GradientStops . Add ( g1 ) ;
1108+
1109+ var g2 = new GradientStop ( c2 , 1.0 ) ;
1110+ myBrush . GradientStops . Add ( g2 ) ;
1111+ rectSaturation . Fill = myBrush ;
1112+
1113+ // set opacity mask
1114+ var opacityBrush = new LinearGradientBrush ( ) ;
1115+ opacityBrush . StartPoint = new Point ( 0 , 0 ) ;
1116+ opacityBrush . EndPoint = new Point ( 0 , 1 ) ;
1117+ var g1b = new GradientStop ( c1 , 0.0 ) ;
1118+ opacityBrush . GradientStops . Add ( g1b ) ;
1119+ c2 . A = 0 ;
1120+ c2 . R = 0 ;
1121+ c2 . G = 0 ;
1122+ c2 . B = 0 ;
1123+ var g2b = new GradientStop ( c2 , 1.0 ) ;
1124+ opacityBrush . GradientStops . Add ( g2b ) ;
1125+ rectSaturation . OpacityMask = opacityBrush ;
1126+
1127+ }
10611128 } // class
10621129
10631130 // https://stackoverflow.com/a/2908885/5452781
0 commit comments