|
11 | 11 | using MahApps.Metro.Controls; |
12 | 12 | using System.ComponentModel; |
13 | 13 | using System.Diagnostics; |
| 14 | +using System.IO; |
14 | 15 | using System.Net; |
15 | 16 | using System.Text.Json; |
| 17 | +using System.Text.RegularExpressions; |
16 | 18 | using System.Windows; |
17 | 19 | using System.Windows.Controls; |
18 | 20 | using System.Windows.Input; |
@@ -657,6 +659,53 @@ private void CopyAsImage_Click(object sender, RoutedEventArgs e) |
657 | 659 | } |
658 | 660 | } |
659 | 661 |
|
| 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 | + |
660 | 709 | private void ExportToHtml_Click(object sender, RoutedEventArgs e) |
661 | 710 | { |
662 | 711 | if (mainViewModel != null && mainViewModel.SelectedSnippet != null) |
@@ -763,6 +812,8 @@ private RenderTargetBitmap RenderSelectionToBitmap(ICSharpCode.AvalonEdit.Editin |
763 | 812 | Foreground = textEditor.Foreground, |
764 | 813 | SyntaxHighlighting = textEditor.SyntaxHighlighting, |
765 | 814 | Text = selectedText, |
| 815 | + HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden, |
| 816 | + VerticalScrollBarVisibility = ScrollBarVisibility.Hidden, |
766 | 817 | Options = textEditor.Options |
767 | 818 | }; |
768 | 819 |
|
|
0 commit comments