Skip to content

Commit 15ea3f4

Browse files
committed
add save as and save (existing file), show current save filepath in window title
1 parent e7ae97a commit 15ea3f4

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

PixelArtTool/MainWindow.xaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<CommandBinding Command="ApplicationCommands.Redo" Executed="Executed_Redo" CanExecute="CanExecute_Redo"/>
1313
<CommandBinding Command="ApplicationCommands.Paste" Executed="Executed_Paste" CanExecute="CanExecute_Paste"/>
1414
<CommandBinding Command="ApplicationCommands.Copy" Executed="Executed_Copy" CanExecute="CanExecute_Copy"/>
15+
<CommandBinding Command="ApplicationCommands.Save" Executed="Executed_Save" CanExecute="CanExecute_Save"/>
1516
<CommandBinding Command="ApplicationCommands.SaveAs" Executed="Executed_SaveAs" CanExecute="CanExecute_SaveAs"/>
1617
<CommandBinding Command="ApplicationCommands.New" Executed="Executed_New" CanExecute="CanExecute_New"/>
1718
</Window.CommandBindings>
@@ -21,6 +22,7 @@
2122
<KeyBinding Command="ApplicationCommands.Paste" Gesture="Ctrl+V"/>
2223
<KeyBinding Command="ApplicationCommands.Copy" Gesture="Ctrl+C"/>
2324
<KeyBinding Command="ApplicationCommands.SaveAs" Gesture="Ctrl+Shift+S"/>
25+
<KeyBinding Command="ApplicationCommands.Save" Gesture="Ctrl+S"/>
2426
<KeyBinding Command="ApplicationCommands.New" Gesture="Ctrl+N"/>
2527
</Window.InputBindings>
2628
<Window.Resources>
@@ -108,7 +110,7 @@
108110
<Button x:Name="btnNew" ToolTip="New (Ctrl+N)" Click="OnClearButton">
109111
<Image Source="/Resources/Buttons/newimage.png" Width="24" Height="24" RenderOptions.BitmapScalingMode="NearestNeighbor" />
110112
</Button>
111-
<Button x:Name="btnSave" ToolTip="Save as..(Ctrl+Shift+s)" Click="OnSaveButton">
113+
<Button x:Name="btnSave" ToolTip="Save (Ctrl+S)" Click="OnSaveButton">
112114
<Image Source="/Resources/Buttons/save.png" Width="24" Height="24" RenderOptions.BitmapScalingMode="NearestNeighbor" />
113115
</Button>
114116
</ToolBar>

PixelArtTool/MainWindow.xaml.cs

+49-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace PixelArtTool
1616
{
1717
public partial class MainWindow : Window, INotifyPropertyChanged
1818
{
19+
string windowTitle = "";
20+
1921
WriteableBitmap canvasBitmap;
2022
WriteableBitmap gridBitmap;
2123
WriteableBitmap outlineBitmap;
@@ -111,6 +113,10 @@ public ToolMode CurrentTool
111113
}
112114
}
113115

116+
// files
117+
string saveFile = null;
118+
119+
114120
public MainWindow()
115121
{
116122
InitializeComponent();
@@ -123,6 +129,8 @@ public MainWindow()
123129

124130
void Start()
125131
{
132+
windowTitle = window.Title;
133+
126134
// needed for binding
127135
DataContext = this;
128136

@@ -594,7 +602,7 @@ void DrawingAreaMouseMoved(object sender, MouseEventArgs e)
594602
// snap preview rectangle to grid
595603
var left = x * canvasScaleX;
596604
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);
598606

599607
} // drawingareamousemoved
600608

@@ -658,26 +666,47 @@ private void OnClearButton(object sender, RoutedEventArgs e)
658666
RegisterUndo();
659667
ClearImage(canvasBitmap, emptyRect, emptyPixels, emptyStride);
660668
UpdateOutline();
669+
// reset title
670+
window.Title = windowTitle;
671+
saveFile = null;
661672
}
662673

674+
// if unsaved, this is same as save as.., if already saved, then overwrite current
663675
private void OnSaveButton(object sender, RoutedEventArgs e)
664676
{
665677
SaveFileDialog saveFileDialog = new SaveFileDialog();
666-
667678
saveFileDialog.FileName = "pixel";
668679
saveFileDialog.DefaultExt = ".png";
669680
saveFileDialog.Filter = "PNG|*.png";
670681
UseDefaultExtensionAsFilterIndex(saveFileDialog);
671682

672-
if (saveFileDialog.ShowDialog() == true)
683+
// save to current file
684+
if (saveFile != null)// || doSaveAs==true)
673685
{
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+
}
680697
}
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+
681710
}
682711

683712
private void OpacitySliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
@@ -852,6 +881,7 @@ public void CanExecute_Copy(object sender, CanExecuteRoutedEventArgs e)
852881

853882
public void Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
854883
{
884+
saveFile = null;
855885
OnSaveButton(null, null);
856886
}
857887

@@ -860,6 +890,16 @@ public void CanExecute_SaveAs(object sender, CanExecuteRoutedEventArgs e)
860890
e.CanExecute = true;
861891
}
862892

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+
863903
public void Executed_New(object sender, ExecutedRoutedEventArgs e)
864904
{
865905
OnClearButton(null, null);

0 commit comments

Comments
 (0)