From 612fb36dfce549c033c111cdd7c6b0698897a532 Mon Sep 17 00:00:00 2001 From: ema Date: Mon, 6 Jan 2025 06:00:13 +0800 Subject: [PATCH] Thread safety of image copying --- .../NativeMethods/ClipboardEx.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/NativeMethods/ClipboardEx.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/NativeMethods/ClipboardEx.cs index 13e14f46..915be3dc 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/NativeMethods/ClipboardEx.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/NativeMethods/ClipboardEx.cs @@ -21,9 +21,7 @@ public static void SetClipboardImage(this BitmapSource img) var thread = new Thread((img) => { if (img == null) - { return; - } var image = (BitmapSource)img; @@ -36,7 +34,7 @@ public static void SetClipboardImage(this BitmapSource img) try { using var pngMemStream = new MemoryStream(); - using var bitmpa = image.ToBitmap(); + using var bitmpa = image.Dispatcher?.Invoke(() => image.ToBitmap()) ?? image.ToBitmap(); var data = new DataObject(); bitmpa.Save(pngMemStream, ImageFormat.Png); @@ -44,7 +42,8 @@ public static void SetClipboardImage(this BitmapSource img) Clipboard.SetDataObject(data, true); } - catch { } + catch { } // Clipboard competition leading to failure is common + // There is currently no UI notification of success or failure }); thread.SetApartmentState(ApartmentState.STA); thread.Start(img);