Fix image viewer copy without transparency support

This commit is contained in:
ema
2024-12-30 05:22:58 +08:00
parent 4eb4251db5
commit 82987b0848
4 changed files with 70 additions and 3 deletions

View File

@@ -0,0 +1,65 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Media.Imaging;
using Clipboard = System.Windows.Forms.Clipboard;
namespace QuickLook.Plugin.ImageViewer.NativeMethods;
internal static class ClipboardEx
{
public static void SetClipboardImage(this BitmapSource img)
{
if (img == null)
{
return;
}
var thread = new Thread((img) =>
{
if (img == null)
{
return;
}
var image = (BitmapSource)img;
try
{
Clipboard.Clear();
}
catch (ExternalException) { }
try
{
using var pngMemStream = new MemoryStream();
using var bitmpa = image.ToBitmap();
var data = new DataObject();
bitmpa.Save(pngMemStream, ImageFormat.Png);
data.SetData("PNG", pngMemStream, false);
Clipboard.SetDataObject(data, true);
}
catch { }
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start(img);
}
private static Bitmap ToBitmap(this BitmapSource source)
{
using (var outStream = new MemoryStream())
{
BitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(source));
enc.Save(outStream);
var bitmap = new Bitmap(outStream);
return new Bitmap(bitmap);
}
}
}