@@ -21,6 +21,8 @@ This source code may use other Open Source software components (see LICENSE.txt)
21
21
using Extensions ;
22
22
using Microsoft . Win32 ;
23
23
using Newtonsoft . Json ;
24
+ using NPOI . HPSF ;
25
+ using Org . BouncyCastle . Asn1 . X509 ;
24
26
using System ;
25
27
using System . Collections . Generic ;
26
28
using System . ComponentModel ;
@@ -2783,27 +2785,52 @@ private void ButtonReport_Click(object sender, RoutedEventArgs e)
2783
2785
}
2784
2786
}
2785
2787
2786
- //private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
2787
- //{
2788
- // // decode
2789
- // var ruic = e?.Command as RoutedUICommand;
2790
- // if (ruic == null)
2791
- // return;
2792
- // var cmd = ruic.Text?.Trim().ToLower();
2788
+ /// <summary>
2789
+ /// Take a screenshot and save to file
2790
+ /// </summary>
2791
+ public void SaveScreenshot ( string filename = "noname" )
2792
+ {
2793
+ // use the whole Window
2794
+ var target = this ;
2795
+ if ( target == null || string . IsNullOrEmpty ( filename ) )
2796
+ {
2797
+ return ;
2798
+ }
2793
2799
2794
- // // see: MainWindow.CommandBindings.cs
2795
- // try
2796
- // {
2797
- // this.CommandBinding_GeneralDispatch(cmd);
2798
- // }
2799
- // catch (Exception err)
2800
- // {
2801
- // throw new InvalidOperationException(
2802
- // $"Failed to execute the command {cmd}: {err}");
2803
- // }
2800
+ // prep filename
2801
+ if ( System . IO . Path . GetExtension ( filename ) == "" )
2802
+ filename += ".png" ;
2804
2803
2805
- //}
2804
+ // needs to be the main thread
2805
+ ProgressBarInfo . Dispatcher . BeginInvoke (
2806
+ System . Windows . Threading . DispatcherPriority . Background ,
2807
+ new Action ( ( ) =>
2808
+ {
2809
+ // see: https://stackoverflow.com/questions/5124825/generating-a-screenshot-of-a-wpf-window
2810
+ Rect bounds = VisualTreeHelper . GetDescendantBounds ( target ) ;
2811
+
2812
+ RenderTargetBitmap renderTarget = new RenderTargetBitmap ( ( Int32 ) bounds . Width , ( Int32 ) bounds . Height , 96 , 96 , PixelFormats . Pbgra32 ) ;
2806
2813
2814
+ DrawingVisual visual = new DrawingVisual ( ) ;
2815
+
2816
+ using ( DrawingContext context = visual . RenderOpen ( ) )
2817
+ {
2818
+ VisualBrush visualBrush = new VisualBrush ( target ) ;
2819
+ context . DrawRectangle ( visualBrush , null , new Rect ( new Point ( ) , bounds . Size ) ) ;
2820
+ }
2821
+
2822
+ renderTarget . Render ( visual ) ;
2823
+ PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder ( ) ;
2824
+ bitmapEncoder . Frames . Add ( BitmapFrame . Create ( renderTarget ) ) ;
2825
+ using ( Stream stm = System . IO . File . Create ( filename ) )
2826
+ {
2827
+ bitmapEncoder . Save ( stm ) ;
2828
+ }
2829
+
2830
+ // Log
2831
+ Log . Singleton . Info ( "Screenshot saved to {0}." , filename ) ;
2832
+ } ) ) ;
2833
+ }
2807
2834
2808
2835
private void DisplayElements_SelectedItemChanged ( object sender , EventArgs e )
2809
2836
{
0 commit comments