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

发布于 2024-07-24 05:05:38 字数 623 浏览 6 评论 0原文

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

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

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

图像保存为 PNG,其中可以包含 Alpha 通道。

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

谁能帮我摆脱痛苦?

提前致谢!

更新:

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

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

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

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.

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫弦 2024-07-31 05:05:38

不要调用 Clipboard.GetImage(),而是尝试调用 Clipboard.GetDataObject()

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

Instead of calling Clipboard.GetImage(), try calling 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.

倦话 2024-07-31 05:05:38

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

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

It might be like this article suggests, 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:

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

你的背包 2024-07-31 05:05:38

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

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

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);
枉心 2024-07-31 05:05:38

我只是使用 Forms 方法。 它不像 Kevin 告诉我们的那样使用 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

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

  '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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文