Skip to content

Commit fe5b243

Browse files
committed
Fix copying compatibility of BitmapFrame
1 parent 895522a commit fe5b243

File tree

1 file changed

+23
-8
lines changed
  • QuickLook.Plugin/QuickLook.Plugin.ImageViewer/NativeMethods

1 file changed

+23
-8
lines changed

Diff for: QuickLook.Plugin/QuickLook.Plugin.ImageViewer/NativeMethods/ClipboardEx.cs

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Drawing;
1+
using System;
2+
using System.Diagnostics;
3+
using System.Drawing;
24
using System.Drawing.Imaging;
35
using System.IO;
46
using System.Runtime.InteropServices;
@@ -14,9 +16,7 @@ internal static class ClipboardEx
1416
public static void SetClipboardImage(this BitmapSource img)
1517
{
1618
if (img == null)
17-
{
1819
return;
19-
}
2020

2121
var thread = new Thread((img) =>
2222
{
@@ -33,18 +33,33 @@ public static void SetClipboardImage(this BitmapSource img)
3333

3434
try
3535
{
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+
3644
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();
3846
var data = new DataObject();
3947

40-
bitmpa.Save(pngMemStream, ImageFormat.Png);
48+
bitmap.Save(pngMemStream, ImageFormat.Png);
4149
data.SetData("PNG", pngMemStream, false);
4250

4351
Clipboard.SetDataObject(data, true);
4452
}
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+
};
4863
thread.SetApartmentState(ApartmentState.STA);
4964
thread.Start(img);
5065
}

0 commit comments

Comments
 (0)