Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...