Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 894
JaffaTheCake Avatar asked Jun 15 '09 22:06

JaffaTheCake


People also ask

How do I get a picture out of my clipboard?

Click the 'Start Menu' icon or press the 'Win' key. Press the 'S' key on your keyboard. Navigate to the image you want to save as JPG or PNG. After locating the image, click the 'New” button in the Snipping Tool app.

How to get image from clipboard in c#?

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


1 Answers

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.

like image 192
Kevin Pullin Avatar answered Sep 20 '22 18:09

Kevin Pullin