From fe5b24319acfe6dbd49d50e8572298d0a91ca668 Mon Sep 17 00:00:00 2001 From: ema Date: Fri, 7 Mar 2025 09:57:06 +0800 Subject: [PATCH] Fix copying compatibility of BitmapFrame --- .../NativeMethods/ClipboardEx.cs | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/NativeMethods/ClipboardEx.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/NativeMethods/ClipboardEx.cs index 915be3d..df44dd3 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/NativeMethods/ClipboardEx.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/NativeMethods/ClipboardEx.cs @@ -1,4 +1,6 @@ -using System.Drawing; +using System; +using System.Diagnostics; +using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Runtime.InteropServices; @@ -14,9 +16,7 @@ internal static class ClipboardEx public static void SetClipboardImage(this BitmapSource img) { if (img == null) - { return; - } var thread = new Thread((img) => { @@ -33,18 +33,33 @@ internal static class ClipboardEx try { + // BitmapFrameDecode or BitmapFrame may need to be converted to + // a standard format of BitmapSource to ensure compatibility + // and only the frozen image is supported + if (image is BitmapFrame && image.IsFrozen) + { + image = new WriteableBitmap(image); + } + using var pngMemStream = new MemoryStream(); - using var bitmpa = image.Dispatcher?.Invoke(() => image.ToBitmap()) ?? image.ToBitmap(); + using var bitmap = image.Dispatcher?.Invoke(() => image.ToBitmap()) ?? image.ToBitmap(); var data = new DataObject(); - bitmpa.Save(pngMemStream, ImageFormat.Png); + bitmap.Save(pngMemStream, ImageFormat.Png); data.SetData("PNG", pngMemStream, false); Clipboard.SetDataObject(data, true); } - catch { } // Clipboard competition leading to failure is common - // There is currently no UI notification of success or failure - }); + catch (Exception e) + { + // Clipboard competition leading to failure is common + // There is currently no UI notification of success or failure + Debug.WriteLine(e); + } + }) + { + Name = nameof(ClipboardEx), + }; thread.SetApartmentState(ApartmentState.STA); thread.Start(img); }