@@ -16,6 +16,8 @@ namespace PixelArtTool
16
16
{
17
17
public partial class MainWindow : Window , INotifyPropertyChanged
18
18
{
19
+ string windowTitle = "" ;
20
+
19
21
WriteableBitmap canvasBitmap ;
20
22
WriteableBitmap gridBitmap ;
21
23
WriteableBitmap outlineBitmap ;
@@ -111,6 +113,10 @@ public ToolMode CurrentTool
111
113
}
112
114
}
113
115
116
+ // files
117
+ string saveFile = null ;
118
+
119
+
114
120
public MainWindow ( )
115
121
{
116
122
InitializeComponent ( ) ;
@@ -123,6 +129,8 @@ public MainWindow()
123
129
124
130
void Start ( )
125
131
{
132
+ windowTitle = window . Title ;
133
+
126
134
// needed for binding
127
135
DataContext = this ;
128
136
@@ -594,7 +602,7 @@ void DrawingAreaMouseMoved(object sender, MouseEventArgs e)
594
602
// snap preview rectangle to grid
595
603
var left = x * canvasScaleX ;
596
604
var top = y * canvasScaleX ;
597
- rectPixelPos . Margin = new Thickness ( 89 + left , 50 + top , 0 , 0 ) ;
605
+ rectPixelPos . Margin = new Thickness ( 89 + left , 50 + top , 0 , 0 ) ;
598
606
599
607
} // drawingareamousemoved
600
608
@@ -658,26 +666,47 @@ private void OnClearButton(object sender, RoutedEventArgs e)
658
666
RegisterUndo ( ) ;
659
667
ClearImage ( canvasBitmap , emptyRect , emptyPixels , emptyStride ) ;
660
668
UpdateOutline ( ) ;
669
+ // reset title
670
+ window . Title = windowTitle ;
671
+ saveFile = null ;
661
672
}
662
673
674
+ // if unsaved, this is same as save as.., if already saved, then overwrite current
663
675
private void OnSaveButton ( object sender , RoutedEventArgs e )
664
676
{
665
677
SaveFileDialog saveFileDialog = new SaveFileDialog ( ) ;
666
-
667
678
saveFileDialog . FileName = "pixel" ;
668
679
saveFileDialog . DefaultExt = ".png" ;
669
680
saveFileDialog . Filter = "PNG|*.png" ;
670
681
UseDefaultExtensionAsFilterIndex ( saveFileDialog ) ;
671
682
672
- if ( saveFileDialog . ShowDialog ( ) == true )
683
+ // save to current file
684
+ if ( saveFile != null ) // || doSaveAs==true)
673
685
{
674
- FileStream stream = new FileStream ( saveFileDialog . FileName , FileMode . Create ) ;
675
- PngBitmapEncoder encoder = new PngBitmapEncoder ( ) ;
676
- encoder . Interlace = PngInterlaceOption . On ;
677
- encoder . Frames . Add ( BitmapFrame . Create ( canvasBitmap ) ) ;
678
- encoder . Save ( stream ) ;
679
- stream . Close ( ) ;
686
+ SaveImageAsPng ( saveFile ) ;
687
+ }
688
+ else // save as
689
+ {
690
+ if ( saveFileDialog . ShowDialog ( ) == true )
691
+ {
692
+ SaveImageAsPng ( saveFileDialog . FileName ) ;
693
+ // update window title
694
+ window . Title = windowTitle + " - " + saveFileDialog . FileName ;
695
+ saveFile = saveFileDialog . FileName ;
696
+ }
680
697
}
698
+
699
+ }
700
+
701
+ void SaveImageAsPng ( string file )
702
+ {
703
+ FileStream stream = new FileStream ( file , FileMode . Create ) ;
704
+ PngBitmapEncoder encoder = new PngBitmapEncoder ( ) ;
705
+ encoder . Interlace = PngInterlaceOption . On ;
706
+ encoder . Frames . Add ( BitmapFrame . Create ( canvasBitmap ) ) ;
707
+ encoder . Save ( stream ) ;
708
+ stream . Close ( ) ;
709
+
681
710
}
682
711
683
712
private void OpacitySliderValueChanged ( object sender , RoutedPropertyChangedEventArgs < double > e )
@@ -852,6 +881,7 @@ public void CanExecute_Copy(object sender, CanExecuteRoutedEventArgs e)
852
881
853
882
public void Executed_SaveAs ( object sender , ExecutedRoutedEventArgs e )
854
883
{
884
+ saveFile = null ;
855
885
OnSaveButton ( null , null ) ;
856
886
}
857
887
@@ -860,6 +890,16 @@ public void CanExecute_SaveAs(object sender, CanExecuteRoutedEventArgs e)
860
890
e . CanExecute = true ;
861
891
}
862
892
893
+ public void Executed_Save ( object sender , ExecutedRoutedEventArgs e )
894
+ {
895
+ OnSaveButton ( null , null ) ;
896
+ }
897
+
898
+ public void CanExecute_Save ( object sender , CanExecuteRoutedEventArgs e )
899
+ {
900
+ e . CanExecute = true ;
901
+ }
902
+
863
903
public void Executed_New ( object sender , ExecutedRoutedEventArgs e )
864
904
{
865
905
OnClearButton ( null , null ) ;
0 commit comments