mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-11 17:59:17 +00:00
Fix image viewer copy without transparency support
This commit is contained in:
@@ -19,6 +19,7 @@ using QuickLook.Common.Annotations;
|
||||
using QuickLook.Common.ExtensionMethods;
|
||||
using QuickLook.Common.Helpers;
|
||||
using QuickLook.Common.Plugin;
|
||||
using QuickLook.Plugin.ImageViewer.NativeMethods;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
@@ -293,13 +294,13 @@ public partial class ImagePanel : UserControl, INotifyPropertyChanged, IDisposab
|
||||
{
|
||||
if (_source is not null)
|
||||
{
|
||||
Clipboard.SetImage(_source);
|
||||
ClipboardEx.SetClipboardImage(_source);
|
||||
return;
|
||||
}
|
||||
|
||||
if (viewPanelImage.Source is BitmapSource bitmapSource)
|
||||
{
|
||||
Clipboard.SetImage(bitmapSource);
|
||||
ClipboardEx.SetClipboardImage(bitmapSource);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@@ -106,7 +106,7 @@ public class MetaProvider
|
||||
}
|
||||
}
|
||||
|
||||
internal static class NativeMethods
|
||||
file static class NativeMethods
|
||||
{
|
||||
private static readonly bool Is64 = Environment.Is64BitProcess;
|
||||
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,6 +8,7 @@
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<SignAssembly>false</SignAssembly>
|
||||
<UseWPF>true</UseWPF>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
|
Reference in New Issue
Block a user