1
- using System . Drawing ;
1
+ using System ;
2
+ using System . Diagnostics ;
3
+ using System . Drawing ;
2
4
using System . Drawing . Imaging ;
3
5
using System . IO ;
4
6
using System . Runtime . InteropServices ;
@@ -14,9 +16,7 @@ internal static class ClipboardEx
14
16
public static void SetClipboardImage ( this BitmapSource img )
15
17
{
16
18
if ( img == null )
17
- {
18
19
return ;
19
- }
20
20
21
21
var thread = new Thread ( ( img ) =>
22
22
{
@@ -33,18 +33,33 @@ public static void SetClipboardImage(this BitmapSource img)
33
33
34
34
try
35
35
{
36
+ // BitmapFrameDecode or BitmapFrame may need to be converted to
37
+ // a standard format of BitmapSource to ensure compatibility
38
+ // and only the frozen image is supported
39
+ if ( image is BitmapFrame && image . IsFrozen )
40
+ {
41
+ image = new WriteableBitmap ( image ) ;
42
+ }
43
+
36
44
using var pngMemStream = new MemoryStream ( ) ;
37
- using var bitmpa = image . Dispatcher ? . Invoke ( ( ) => image . ToBitmap ( ) ) ?? image . ToBitmap ( ) ;
45
+ using var bitmap = image . Dispatcher ? . Invoke ( ( ) => image . ToBitmap ( ) ) ?? image . ToBitmap ( ) ;
38
46
var data = new DataObject ( ) ;
39
47
40
- bitmpa . Save ( pngMemStream , ImageFormat . Png ) ;
48
+ bitmap . Save ( pngMemStream , ImageFormat . Png ) ;
41
49
data . SetData ( "PNG" , pngMemStream , false ) ;
42
50
43
51
Clipboard . SetDataObject ( data , true ) ;
44
52
}
45
- catch { } // Clipboard competition leading to failure is common
46
- // There is currently no UI notification of success or failure
47
- } ) ;
53
+ catch ( Exception e )
54
+ {
55
+ // Clipboard competition leading to failure is common
56
+ // There is currently no UI notification of success or failure
57
+ Debug . WriteLine ( e ) ;
58
+ }
59
+ } )
60
+ {
61
+ Name = nameof ( ClipboardEx ) ,
62
+ } ;
48
63
thread . SetApartmentState ( ApartmentState . STA ) ;
49
64
thread . Start ( img ) ;
50
65
}
0 commit comments