@@ -97,6 +97,12 @@ public static implicit operator Point(POINT point)
97
97
// modes
98
98
BlendMode blendMode ;
99
99
100
+ // clear buffers
101
+ Int32Rect emptyRect ;
102
+ int bytesPerPixel ;
103
+ byte [ ] emptyPixels ;
104
+ int emptyStride ;
105
+
100
106
private ToolMode _currentTool = ToolMode . Draw ;
101
107
public ToolMode CurrentTool
102
108
{
@@ -128,7 +134,6 @@ void Start()
128
134
RenderOptions . SetBitmapScalingMode ( gridImage , BitmapScalingMode . NearestNeighbor ) ;
129
135
RenderOptions . SetEdgeMode ( gridImage , EdgeMode . Aliased ) ;
130
136
w = ( MainWindow ) Application . Current . MainWindow ;
131
- //var gridScaleX = (int)gridImage.Width / canvasResolutionX;
132
137
gridBitmap = new WriteableBitmap ( canvasResolutionX , canvasResolutionY , dpiX , dpiY , PixelFormats . Bgra32 , null ) ;
133
138
gridImage . Source = gridBitmap ;
134
139
DrawBackgroundGrid ( ) ;
@@ -138,7 +143,6 @@ void Start()
138
143
RenderOptions . SetBitmapScalingMode ( outlineImage , BitmapScalingMode . NearestNeighbor ) ;
139
144
RenderOptions . SetEdgeMode ( outlineImage , EdgeMode . Aliased ) ;
140
145
w = ( MainWindow ) Application . Current . MainWindow ;
141
- //var gridScaleX = (int)gridImage.Width / canvasResolutionX;
142
146
outlineBitmap = new WriteableBitmap ( canvasResolutionX , canvasResolutionY , dpiX , dpiY , PixelFormats . Bgra32 , null ) ;
143
147
outlineImage . Source = outlineBitmap ;
144
148
@@ -151,6 +155,12 @@ void Start()
151
155
canvasBitmap = new WriteableBitmap ( canvasResolutionX , canvasResolutionY , dpiX , dpiY , PixelFormats . Bgra32 , null ) ;
152
156
drawingImage . Source = canvasBitmap ;
153
157
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
+
154
164
// setup preview area
155
165
RenderOptions . SetBitmapScalingMode ( imgPreview1x , BitmapScalingMode . NearestNeighbor ) ;
156
166
imgPreview1x . Source = canvasBitmap ;
@@ -663,13 +673,13 @@ void drawingMouseWheel(object sender, MouseWheelEventArgs e)
663
673
private void OnClearButton ( object sender , RoutedEventArgs e )
664
674
{
665
675
ClearImage ( canvasBitmap ) ;
676
+ UpdateOutline ( ) ;
666
677
}
667
678
668
679
// clears bitmap by re-creating it
669
- void ClearImage ( WriteableBitmap target )
680
+ void ClearImage ( WriteableBitmap targetBitmap )
670
681
{
671
- canvasBitmap = new WriteableBitmap ( canvasResolutionX , canvasResolutionY , dpiX , dpiY , PixelFormats . Bgra32 , null ) ;
672
- drawingImage . Source = canvasBitmap ;
682
+ targetBitmap . WritePixels ( emptyRect , emptyPixels , emptyStride , 0 ) ;
673
683
}
674
684
675
685
private void OnSaveButton ( object sender , RoutedEventArgs e )
@@ -747,7 +757,7 @@ void OnKeyDown(object sender, KeyEventArgs e)
747
757
c2 . Blue = c1 . B ;
748
758
currentColor = c2 ;
749
759
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);
751
761
break ;
752
762
case Key . X : // swap current/secondary colors
753
763
var tempcolor = rectCurrentColor . Fill ;
@@ -1058,6 +1068,63 @@ public static Point GetCursorPosition()
1058
1068
return lpPoint ;
1059
1069
}
1060
1070
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
+ }
1061
1128
} // class
1062
1129
1063
1130
// https://stackoverflow.com/a/2908885/5452781
0 commit comments