33using System . Windows ;
44using System . Windows . Controls ;
55using System . Windows . Ink ;
6- using System . Windows . Input ;
76using System . Windows . Media ;
87using System . Windows . Media . Imaging ;
9- using System . Windows . Media . Effects ;
108using Microsoft . Win32 ;
119using DevTools . Helpers ;
1210using DevTools . Resources ;
13- using Button = System . Windows . Controls . Button ;
1411using MessageBox = System . Windows . MessageBox ;
1512using SaveFileDialog = Microsoft . Win32 . SaveFileDialog ;
16- using Cursors = System . Windows . Input . Cursors ;
17- using Orientation = System . Windows . Controls . Orientation ;
18- using HorizontalAlignment = System . Windows . HorizontalAlignment ;
19- using Image = System . Windows . Controls . Image ;
20- using Color = System . Windows . Media . Color ;
21- using Brushes = System . Windows . Media . Brushes ;
22- using Application = System . Windows . Application ;
23- using FontFamily = System . Windows . Media . FontFamily ;
24- using Point = System . Windows . Point ;
2513
2614namespace DevTools . Pages
2715{
2816 public partial class SignaturePage : Page
2917 {
30- private byte [ ] ? _currentImageBytes ;
3118 private BitmapSource ? _currentImage ;
3219
3320 public SignaturePage ( )
@@ -45,27 +32,18 @@ private void PenSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEven
4532 {
4633 PenSizeText . Text = e . NewValue . ToString ( "F0" ) ;
4734 DefaultPenAttributes . Width = e . NewValue ;
35+ DefaultPenAttributes . Height = e . NewValue ;
4836 }
4937
5038 private void Clear_Click ( object sender , RoutedEventArgs e )
5139 {
5240 SignatureCanvas . Strokes . Clear ( ) ;
53- ClearResult ( ) ;
54- }
55-
56- private void ClearResult ( )
57- {
5841 Base64Text . Text = string . Empty ;
59- ImageDimensionsText . Text = "-" ;
60- ImageSizeText . Text = "-" ;
61- _currentImageBytes = null ;
6242 _currentImage = null ;
63- ResultImage . Source = null ;
6443 SaveImageButton . Visibility = Visibility . Collapsed ;
65- ImageBorder . Visibility = Visibility . Collapsed ;
6644 }
6745
68- private void Generate_Click ( object sender , RoutedEventArgs e )
46+ private void Convert_Click ( object sender , RoutedEventArgs e )
6947 {
7048 if ( SignatureCanvas . Strokes . Count == 0 )
7149 {
@@ -99,17 +77,10 @@ private void Generate_Click(object sender, RoutedEventArgs e)
9977
10078 using var stream = new MemoryStream ( ) ;
10179 encoder . Save ( stream ) ;
102- _currentImageBytes = stream . ToArray ( ) ;
80+ var imageBytes = stream . ToArray ( ) ;
10381
104- var base64 = Convert . ToBase64String ( _currentImageBytes ) ;
105- Base64Text . Text = base64 ;
106-
107- ImageDimensionsText . Text = $ "{ bitmap . PixelWidth } x { bitmap . PixelHeight } ";
108- ImageSizeText . Text = FormatBytes ( _currentImageBytes . Length ) ;
109-
110- ResultImage . Source = _currentImage ;
82+ Base64Text . Text = Convert . ToBase64String ( imageBytes ) ;
11183 SaveImageButton . Visibility = Visibility . Visible ;
112- ImageBorder . Visibility = Visibility . Visible ;
11384 }
11485 catch ( Exception ex )
11586 {
@@ -136,109 +107,6 @@ private void SaveImage_Click(object sender, RoutedEventArgs e)
136107 return ;
137108 }
138109
139- SaveImage ( _currentImage ) ;
140- }
141-
142- private void ImageBorder_MouseLeftButtonUp ( object sender , MouseButtonEventArgs e )
143- {
144- if ( _currentImage == null )
145- {
146- MessageBox . Show ( Strings . NoImageToPreview , Strings . Info , MessageBoxButton . OK , MessageBoxImage . Information ) ;
147- return ;
148- }
149-
150- var previewWindow = new Window
151- {
152- Title = $ "{ Strings . ImagePreview } - { _currentImage . PixelWidth } x { _currentImage . PixelHeight } ",
153- Width = Math . Min ( _currentImage . PixelWidth + 40 , SystemParameters . WorkArea . Width * 0.9 ) ,
154- Height = Math . Min ( _currentImage . PixelHeight + 100 , SystemParameters . WorkArea . Height * 0.9 ) ,
155- WindowStartupLocation = WindowStartupLocation . CenterScreen ,
156- Background = new SolidColorBrush ( Color . FromRgb ( 18 , 18 , 18 ) )
157- } ;
158-
159- var grid = new Grid ( ) ;
160- grid . RowDefinitions . Add ( new RowDefinition { Height = GridLength . Auto } ) ;
161- grid . RowDefinitions . Add ( new RowDefinition { Height = new GridLength ( 1 , GridUnitType . Star ) } ) ;
162-
163- var toolbar = new StackPanel
164- {
165- Orientation = Orientation . Horizontal ,
166- HorizontalAlignment = HorizontalAlignment . Right ,
167- Margin = new Thickness ( 10 )
168- } ;
169-
170- var saveButton = CreateIconButton ( ) ;
171- saveButton . Click += ( s , args ) => SaveImage ( _currentImage ) ;
172- toolbar . Children . Add ( saveButton ) ;
173-
174- grid . Children . Add ( toolbar ) ;
175- Grid . SetRow ( toolbar , 0 ) ;
176-
177- var image = new Image
178- {
179- Source = _currentImage ,
180- Stretch = Stretch . Uniform ,
181- Margin = new Thickness ( 10 )
182- } ;
183-
184- grid . Children . Add ( image ) ;
185- Grid . SetRow ( image , 1 ) ;
186-
187- previewWindow . Content = grid ;
188- previewWindow . ShowDialog ( ) ;
189- }
190-
191- private Button CreateIconButton ( )
192- {
193- var textBlock = new TextBlock
194- {
195- FontFamily = ( FontFamily ) Application . Current . FindResource ( "FontAwesomeSolid" ) ,
196- Text = "\uf0c7 " ,
197- FontSize = 18 ,
198- Foreground = Brushes . White ,
199- RenderTransformOrigin = new Point ( 0.5 , 0.5 )
200- } ;
201-
202- var button = new Button
203- {
204- Content = textBlock ,
205- Width = 40 ,
206- Height = 40 ,
207- Cursor = System . Windows . Input . Cursors . Hand ,
208- Background = new SolidColorBrush ( Color . FromRgb ( 74 , 74 , 74 ) ) ,
209- BorderThickness = new Thickness ( 0 ) ,
210- ToolTip = Strings . SaveImage
211- } ;
212-
213- var template = new ControlTemplate ( typeof ( Button ) ) ;
214- var borderFactory = new FrameworkElementFactory ( typeof ( Border ) ) ;
215- borderFactory . Name = "border" ;
216- borderFactory . SetValue ( Border . BackgroundProperty , new TemplateBindingExtension ( Button . BackgroundProperty ) ) ;
217- borderFactory . SetValue ( Border . CornerRadiusProperty , new CornerRadius ( 20 ) ) ;
218- borderFactory . SetValue ( Border . SnapsToDevicePixelsProperty , true ) ;
219-
220- var contentFactory = new FrameworkElementFactory ( typeof ( ContentPresenter ) ) ;
221- contentFactory . SetValue ( ContentPresenter . HorizontalAlignmentProperty , HorizontalAlignment . Center ) ;
222- contentFactory . SetValue ( ContentPresenter . VerticalAlignmentProperty , VerticalAlignment . Center ) ;
223-
224- borderFactory . AppendChild ( contentFactory ) ;
225- template . VisualTree = borderFactory ;
226-
227- var hoverTrigger = new Trigger { Property = Button . IsMouseOverProperty , Value = true } ;
228- hoverTrigger . Setters . Add ( new Setter ( Border . BackgroundProperty , new SolidColorBrush ( Color . FromRgb ( 90 , 90 , 90 ) ) , "border" ) ) ;
229- hoverTrigger . Setters . Add ( new Setter ( Border . EffectProperty , new DropShadowEffect { Color = Color . FromArgb ( 102 , 0 , 0 , 0 ) , BlurRadius = 12 , ShadowDepth = 2 } , "border" ) ) ;
230- template . Triggers . Add ( hoverTrigger ) ;
231-
232- var pressedTrigger = new Trigger { Property = Button . IsPressedProperty , Value = true } ;
233- pressedTrigger . Setters . Add ( new Setter ( Border . BackgroundProperty , new SolidColorBrush ( Color . FromRgb ( 106 , 106 , 106 ) ) , "border" ) ) ;
234- template . Triggers . Add ( pressedTrigger ) ;
235-
236- button . Template = template ;
237- return button ;
238- }
239-
240- private void SaveImage ( BitmapSource image )
241- {
242110 var dialog = new SaveFileDialog
243111 {
244112 Filter = "PNG Image|*.png|JPEG Image|*.jpg|Bitmap Image|*.bmp|GIF Image|*.gif|All Files|*.*" ,
@@ -258,7 +126,7 @@ private void SaveImage(BitmapSource image)
258126 _ => new PngBitmapEncoder ( )
259127 } ;
260128
261- encoder . Frames . Add ( BitmapFrame . Create ( image ) ) ;
129+ encoder . Frames . Add ( BitmapFrame . Create ( _currentImage ) ) ;
262130
263131 using var stream = new FileStream ( dialog . FileName , FileMode . Create ) ;
264132 encoder . Save ( stream ) ;
@@ -271,18 +139,5 @@ private void SaveImage(BitmapSource image)
271139 }
272140 }
273141 }
274-
275- private string FormatBytes ( long bytes )
276- {
277- string [ ] sizes = { "B" , "KB" , "MB" , "GB" } ;
278- double len = bytes ;
279- int order = 0 ;
280- while ( len >= 1024 && order < sizes . Length - 1 )
281- {
282- order ++ ;
283- len = len / 1024 ;
284- }
285- return $ "{ len : 0.##} { sizes [ order ] } ";
286- }
287142 }
288143}
0 commit comments