Skip to content

Commit

Permalink
Add open image and open folder buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJoeFin committed Apr 4, 2024
1 parent 194455a commit 0276a98
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
25 changes: 22 additions & 3 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<GeometryModel3D.Material>
<DiffuseMaterial>
<DiffuseMaterial.Brush>
<ImageBrush ImageSource="LetterPaperTest.jpg" />
<ImageBrush x:Name="MainImage" ImageSource="LetterPaperTest.jpg" />
</DiffuseMaterial.Brush>
</DiffuseMaterial>
</GeometryModel3D.Material>
Expand Down Expand Up @@ -74,11 +74,30 @@
</Viewbox>

<Button
x:Name="OpenButton"
Margin="20"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="OpenButton_Click"
Content="Open Image" />

<StackPanel
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Click="SaveButton_Click"
Content="Save" />
Orientation="Horizontal">
<Button
x:Name="OpenFolder"
Margin="5,20"
Click="OpenFolder_Click"
Content="Open Folder"
IsEnabled="False" />

<Button
x:Name="SaveButton"
Margin="5,20,20,20"
Click="SaveButton_Click"
Content="Save" />
</StackPanel>
</Grid>

</Window>
35 changes: 33 additions & 2 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -205,13 +207,42 @@ public void SaveButton_Click(object sender, RoutedEventArgs e)
return;

string imageFileName = $"{folder}\\tempFile.png";
button.Content = imageFileName;

if (File.Exists(imageFileName))
File.Delete(imageFileName);

using FileStream fs = new(imageFileName, FileMode.Create);
encoder.Save(fs);
OpenFolder.IsEnabled = true;
button.Content = "Saved as \"tempFile.png\"";
}

private void OpenButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new()
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
Filter = "PNG Files(*.png)|*.png|JPEG Files(*.jpg;*.jpeg)|*.jpg;*.jpeg|All files (*.*)|*.*"
};

if (openFileDialog.ShowDialog() != true)
return;

BitmapImage bitmap = new();
bitmap.BeginInit();
bitmap.UriSource = new(openFileDialog.FileName);
bitmap.EndInit();
MainImage.ImageSource = bitmap;
}

private void OpenFolder_Click(object sender, RoutedEventArgs e)
{
string? folder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

if (folder is null)
return;

Process.Start("explorer.exe", folder);
}
}

0 comments on commit 0276a98

Please sign in to comment.