C# 如何在不丢失 .NET 中的 alpha 通道的情况下从剪贴板中获取图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/998655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:11:20  来源:igfitidea点击:

How can I get an image out of the clipboard without losing the alpha channel in .NET?

c#.netimagepngclipboard

提问by JaffaTheCake

I'm trying to save a copied image from the clipboard but it's losing its alpha channel:

我正在尝试从剪贴板保存复制的图像,但它正在丢失其 alpha 通道:

Image clipboardImage = Clipboard.GetImage();
string imagePath = Path.GetTempFileName();
clipboardImage.Save(imagePath);

If I copy a 32bit image from PhotoShop or IE/Firefox/Chrome and run the above code, the output loses its alpha channel, instead it is saved against a black background.

如果我从 PhotoShop 或 IE/Firefox/Chrome 复制 32 位图像并运行上述代码,输出将丢失其 alpha 通道,而是保存在黑色背景下。

The image is saved as PNG, which can contain an alpha channel.

图像保存为 PNG,它可以包含一个 alpha 通道。

The correct data appears to be in the clipboard because pasting into other applications (such as PhotoShop) retains the alpha channel.

正确的数据似乎在剪贴板中,因为粘贴到其他应用程序(如 PhotoShop)中会保留 Alpha 通道。

Can anyone put me out of my misery?

谁能把我从痛苦中解脱出来?

Thanks in advance!

提前致谢!

Update:

更新:

// outputs FALSE
Debug.WriteLine(Image.IsAlphaPixelFormat(Clipboard.GetImage().PixelFormat));

The above suggests that the alpha data is lost as soon as it's taken out of the clipboard. Perhaps I need to get it out of the clipboard some other way?

以上表明 alpha 数据一旦从剪贴板中取出就会丢失。也许我需要以其他方式将其从剪贴板中取出?

采纳答案by Kevin Pullin

Instead of calling Clipboard.GetImage(), try calling Clipboard.GetDataObject()

与其打电话Clipboard.GetImage(),不如试试打电话Clipboard.GetDataObject()

This returns an IDataObject, which you can in turn query by calling dataObject.GetFormats(). GetFormats()returns the type formats supported by the Clipboard object - there may be a more precise format supported that you can use to extract the data.

这将返回一个 IDataObject,您可以依次调用dataObject.GetFormats(). GetFormats()返回 Clipboard 对象支持的类型格式 - 可能支持更精确的格式,您可以使用它来提取数据。

回答by Jim

The image is being saved as a bitmap where the transparent pixels are visible on the clipboard so use this code

图像被保存为位图,其中透明像素在剪贴板上可见,因此请使用此代码

Bitmap clipboardImage = Clipboard.GetImage();
clipboardImage.MakeTransparent()
string imagePath = Path.GetTempFileName();
clipboardImage.Save(imagePath);

回答by Henrik

It might be like this articlesuggests, that the Clipboard object, working within Win32, is only able to manage bitmaps, which don't feature the transparent/partially transparent alpha channel. The OLE clipboard is more capable, it seems:

可能就像这篇文章所暗示的那样,在 Win32 中工作的 Clipboard 对象只能管理位图,而位图不具有透明/部分透明的 alpha 通道。OLE 剪贴板功能更强大,似乎:

However, the netezwas the best article I found on the topic. (beware I haven't tested this myself)

但是,netez是我在该主题上找到的最好的文章。(当心我自己没有测试过)

回答by Nasenbaer

I am just using Forms methode. Its not that nice solution like using GetFormatlike Kevin is telling us but its more quick and works quiete well at all.

我只是使用表单方法。它不是GetFormat像凯文告诉我们的那样使用的好解决方案,但它更快速并且工作起来非常安静。

  'Dim bm As BitmapSource = Clipboard.GetImage()'looses alpha channel
                'Dim bmS As New WriteableBitmap(bm)'does work but still without alpha information
                Dim bmF As System.Drawing.Bitmap = System.Windows.Forms.Clipboard.GetImage 'Get working image
                Dim bmS As BitmapSource = TB.Imaging.WPF.BitmapToWpfBitmapSource(bmF, Me) 'convert Bitmap into BitmapSource
                Me.Source = bmS