11using System ;
22using System . Collections . Generic ;
3+ using System . Diagnostics ;
34using System . Drawing ;
45using System . IO ;
56using System . Runtime . InteropServices ;
@@ -237,13 +238,21 @@ static ScreenCut()
237238 public void Dispose ( )
238239 {
239240 _canvas . Background = null ;
240- GC . SuppressFinalize ( this ) ;
241+ GC . Collect ( ) ;
242+ GC . WaitForPendingFinalizers ( ) ;
241243 GC . Collect ( ) ;
242244 }
245+
243246 public static void ClearCaptureScreenID ( )
244247 {
245248 CaptureScreenID = - 1 ;
246249 }
250+
251+ ~ ScreenCut ( )
252+ {
253+ Debug . WriteLine ( "~ScreenCut" ) ;
254+ }
255+
247256 public override void OnApplyTemplate ( )
248257 {
249258 base . OnApplyTemplate ( ) ;
@@ -298,7 +307,17 @@ public override void OnApplyTemplate()
298307 _wrapPanel . PreviewMouseDown += WrapPanel_PreviewMouseDown ;
299308 Loaded += ScreenCut_Loaded ;
300309 _controlTemplate = ( ControlTemplate ) FindResource ( "WD.PART_DrawArrow" ) ;
301- _canvas . Background = new ImageBrush ( ImagingHelper . CreateBitmapSourceFromBitmap ( CopyScreen ( ) ) ) ;
310+ _screenCapture = CopyScreen ( ) ;
311+ using ( var tempBitmap = _screenCapture )
312+ {
313+ var imageSource = ImagingHelper . CreateBitmapSourceFromBitmap ( tempBitmap ) ;
314+ imageSource . Freeze ( ) ;
315+ var writeableBitmap = new WriteableBitmap ( imageSource ) ;
316+ writeableBitmap . Freeze ( ) ;
317+ _canvas . Background = new ImageBrush ( writeableBitmap ) ;
318+ }
319+ _screenCapture ? . Dispose ( ) ;
320+ _screenCapture = null ;
302321 TakeSnapshot ( ) ;
303322 }
304323
@@ -310,6 +329,22 @@ private void OnUndoButton_Click(object sender, RoutedEventArgs e)
310329 protected override void OnClosed ( EventArgs e )
311330 {
312331 base . OnClosed ( e ) ;
332+ if ( _adornerLayer != null && _screenCutAdorner != null )
333+ {
334+ _adornerLayer . Remove ( _screenCutAdorner ) ;
335+ _screenCutAdorner = null ;
336+ _adornerLayer = null ;
337+ }
338+ if ( _canvas != null )
339+ {
340+ if ( _canvas . Background is ImageBrush brush )
341+ {
342+ brush . ImageSource = null ;
343+ }
344+ _canvas . Background = null ;
345+ _canvas . Children . Clear ( ) ;
346+ }
347+ _imageSnapshot = null ;
313348 Dispose ( ) ;
314349 }
315350
@@ -766,6 +801,7 @@ private void TakeSnapshot()
766801 96 , 96 , PixelFormats . Pbgra32 ) ;
767802
768803 _imageSnapshot . Render ( _canvas ) ;
804+ _imageSnapshot . Freeze ( ) ;
769805 }
770806
771807 private void DrawMosaicBlock ( Point center , int blockSize , int brushSize )
0 commit comments