Skip to content

Commit 9dbe619

Browse files
committed
Export to Image...
1 parent 38cbad5 commit 9dbe619

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/CodeSnip/MainWindow.xaml.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
using MahApps.Metro.Controls;
1212
using System.ComponentModel;
1313
using System.Diagnostics;
14+
using System.IO;
1415
using System.Net;
1516
using System.Text.Json;
17+
using System.Text.RegularExpressions;
1618
using System.Windows;
1719
using System.Windows.Controls;
1820
using System.Windows.Input;
@@ -657,6 +659,53 @@ private void CopyAsImage_Click(object sender, RoutedEventArgs e)
657659
}
658660
}
659661

662+
private void ExportToPng_Click(object sender, RoutedEventArgs e)
663+
{
664+
if (textEditor.TextArea.Selection is not ICSharpCode.AvalonEdit.Editing.RectangleSelection selection || selection.IsEmpty)
665+
{
666+
MessageBox.Show("This action requires a rectangular selection (Alt + Mouse Drag or Alt + Shift + Arrow Keys).", "No Rectangular Selection", MessageBoxButton.OK, MessageBoxImage.Information);
667+
return;
668+
}
669+
670+
if (mainViewModel.SelectedSnippet == null)
671+
{
672+
MessageBox.Show("Please select a snippet first to determine the filename.", "No Snippet Selected", MessageBoxButton.OK, MessageBoxImage.Warning);
673+
return;
674+
}
675+
676+
try
677+
{
678+
var bitmap = RenderSelectionToBitmap(selection);
679+
680+
var encoder = new PngBitmapEncoder();
681+
encoder.Frames.Add(BitmapFrame.Create(bitmap));
682+
683+
string exportsDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Exports");
684+
Directory.CreateDirectory(exportsDir);
685+
686+
string snippetTitle = mainViewModel.SelectedSnippet.Title;
687+
string invalidChars = new string(Path.GetInvalidFileNameChars());
688+
foreach (char c in invalidChars)
689+
{
690+
snippetTitle = snippetTitle.Replace(c.ToString(), "_");
691+
}
692+
// Replace one or more whitespace characters with a single underscore
693+
snippetTitle = Regex.Replace(snippetTitle, @"\s+", "_");
694+
string filePath = Path.Combine(exportsDir, $"{snippetTitle}.png");
695+
696+
using (var fileStream = new FileStream(filePath, FileMode.Create))
697+
{
698+
encoder.Save(fileStream);
699+
}
700+
701+
MessageBox.Show($"Image successfully saved to:\n{filePath}", "Export Successful", MessageBoxButton.OK, MessageBoxImage.Information);
702+
}
703+
catch (Exception ex)
704+
{
705+
MessageBox.Show($"Failed to export selection as image: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
706+
}
707+
}
708+
660709
private void ExportToHtml_Click(object sender, RoutedEventArgs e)
661710
{
662711
if (mainViewModel != null && mainViewModel.SelectedSnippet != null)
@@ -763,6 +812,8 @@ private RenderTargetBitmap RenderSelectionToBitmap(ICSharpCode.AvalonEdit.Editin
763812
Foreground = textEditor.Foreground,
764813
SyntaxHighlighting = textEditor.SyntaxHighlighting,
765814
Text = selectedText,
815+
HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden,
816+
VerticalScrollBarVisibility = ScrollBarVisibility.Hidden,
766817
Options = textEditor.Options
767818
};
768819

0 commit comments

Comments
 (0)