Skip to content

Commit 0276a98

Browse files
committed
Add open image and open folder buttons
1 parent 194455a commit 0276a98

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

MainWindow.xaml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<GeometryModel3D.Material>
2929
<DiffuseMaterial>
3030
<DiffuseMaterial.Brush>
31-
<ImageBrush ImageSource="LetterPaperTest.jpg" />
31+
<ImageBrush x:Name="MainImage" ImageSource="LetterPaperTest.jpg" />
3232
</DiffuseMaterial.Brush>
3333
</DiffuseMaterial>
3434
</GeometryModel3D.Material>
@@ -74,11 +74,30 @@
7474
</Viewbox>
7575

7676
<Button
77+
x:Name="OpenButton"
7778
Margin="20"
79+
HorizontalAlignment="Left"
80+
VerticalAlignment="Top"
81+
Click="OpenButton_Click"
82+
Content="Open Image" />
83+
84+
<StackPanel
7885
HorizontalAlignment="Right"
7986
VerticalAlignment="Bottom"
80-
Click="SaveButton_Click"
81-
Content="Save" />
87+
Orientation="Horizontal">
88+
<Button
89+
x:Name="OpenFolder"
90+
Margin="5,20"
91+
Click="OpenFolder_Click"
92+
Content="Open Folder"
93+
IsEnabled="False" />
94+
95+
<Button
96+
x:Name="SaveButton"
97+
Margin="5,20,20,20"
98+
Click="SaveButton_Click"
99+
Content="Save" />
100+
</StackPanel>
82101
</Grid>
83102

84103
</Window>

MainWindow.xaml.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
using Microsoft.Win32;
2+
using System;
3+
using System.Diagnostics;
24
using System.IO;
35
using System.Windows;
46
using System.Windows.Controls;
@@ -205,13 +207,42 @@ public void SaveButton_Click(object sender, RoutedEventArgs e)
205207
return;
206208

207209
string imageFileName = $"{folder}\\tempFile.png";
208-
button.Content = imageFileName;
209210

210211
if (File.Exists(imageFileName))
211212
File.Delete(imageFileName);
212213

213214
using FileStream fs = new(imageFileName, FileMode.Create);
214215
encoder.Save(fs);
216+
OpenFolder.IsEnabled = true;
217+
button.Content = "Saved as \"tempFile.png\"";
218+
}
219+
220+
private void OpenButton_Click(object sender, RoutedEventArgs e)
221+
{
222+
OpenFileDialog openFileDialog = new()
223+
{
224+
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
225+
Filter = "PNG Files(*.png)|*.png|JPEG Files(*.jpg;*.jpeg)|*.jpg;*.jpeg|All files (*.*)|*.*"
226+
};
227+
228+
if (openFileDialog.ShowDialog() != true)
229+
return;
230+
231+
BitmapImage bitmap = new();
232+
bitmap.BeginInit();
233+
bitmap.UriSource = new(openFileDialog.FileName);
234+
bitmap.EndInit();
235+
MainImage.ImageSource = bitmap;
236+
}
237+
238+
private void OpenFolder_Click(object sender, RoutedEventArgs e)
239+
{
240+
string? folder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
241+
242+
if (folder is null)
243+
return;
244+
245+
Process.Start("explorer.exe", folder);
215246
}
216247
}
217248

0 commit comments

Comments
 (0)