Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET: Saving Clipboard Image to PNG fails

I have a weird issue with saving of clipboard image to png. When taking a screenshot with prtscr or snipping tool it works fine with Clipboard.GetImage() and PngBitmapEncoder. But when copying the image from Paint and doing the same I get a empty image as result. At least it looks empty when trying to view it, though it seems to contain valid PNG-image-data. When using the Jpeg-encoder it works fine. Whats the problem with PNG and Paint? Does it have anything to do with transparency?

Update to clarify: Im extracting an image successfully from the clipboard. I just cant save it to disc using the PNGEncoder if the image was copied from Paint in the first place. I can save paint-clipboard-images with the JpegEncoder though... but I want to use PNG.

BitmapSource source = Clipboard.GetImage();
PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(source));
enc.Save(stream);

Above code produces a image-file with invisible content, turns up completely black or white depending on the viewer (when copied from, ctrl-c, from Paint). Which leads me to think it has something to do with transparency...

like image 346
Andreas Zita Avatar asked Apr 07 '11 11:04

Andreas Zita


1 Answers

Your problem looks very similar to the one I described here... the solution I suggested should probably work for you.

like image 170
Thomas Levesque Avatar answered Sep 23 '22 11:09

Thomas Levesque