4

I am trying to save a PNG image that has been copied to the clipboard, but it is either turning out as a solid black, or black around the areas that should be transparent.

Here is the code I am using to capture and save the Image

var clipboardImage = (InteropBitmap)Clipboard.GetImage();

Image.SaveImage(clipboardImage, Path.Combine(Config.App.ApplicationDataImagesPath, string.Format("{0}.{1}", imageId, "png")));

public static void SaveImage(BitmapSource bitmapImage, string filename)
{
    using (var fileStream = new FileStream(filename, FileMode.Create))
    {
        var pngBitmapEncoder = new PngBitmapEncoder();
        pngBitmapEncoder.Frames.Add(BitmapFrame.Create(bitmapImage));
        pngBitmapEncoder.Save(fileStream);
        fileStream.Close();
        fileStream.Dispose();
    }           
}

Does anyone have any ideas why it won't persrve the alpha channels of a PNG?

Thanks

Dan

Edit: I should of mentioned that black images were happening when copying an image from Internet Explorer 9. Works perfectly when copying an image from either Chrome or Firefox. Any workarounds for IE9 issue?

Daniel Clark
  • 615
  • 2
  • 9
  • 17
  • You're extracting the image on the clipboard as if it is a bitmap (a regular .BMP), which does not support transparency like a PNG does. You need to extract it as a PNG to preserve transparency. – Cody Gray - on strike Jul 20 '11 at 23:44
  • you Close() and Dispose() the filestream which is not necessary because it is wrapped in "using"... I am not sure if this can even be "harmful"... – Yahia Jul 20 '11 at 23:56
  • @Yahia, considered Closer() and Dipose() removed. No idea why I had them there in the first place. – Daniel Clark Jul 21 '11 at 00:11
  • @Cody Any examples of how to extract the image as a PNG? – Daniel Clark Jul 21 '11 at 00:11
  • Yes, indeed. See the answers here: [How can I get an image out of the clipboard without losing the alpha channel in .NET?](http://stackoverflow.com/questions/998655/how-can-i-get-an-image-out-of-the-clipboard-without-losing-the-alpha-channel-in) Or perhaps the solution detailed [here](http://tomlev2.wordpress.com/2009/02/05/wpf-paste-an-image-from-the-clipboard/). – Cody Gray - on strike Jul 21 '11 at 02:31
  • Thanks! Will check out today. The first link looks ideal... will let you know how it goes. Thanks again. – Daniel Clark Jul 21 '11 at 09:56
  • The code sample from the first link still produces an image filled with black where it should be transparent... example: http://i.imgur.com/wnZ29.png – Daniel Clark Jul 21 '11 at 10:07
  • Just to make myself clear, I meant the second link - http://tomlev2.wordpress.com/2009/02/05/wpf-paste-an-image-from-the-clipboard/ – Daniel Clark Jul 21 '11 at 10:52

1 Answers1

3

What happens if just do this:

Clipboard.GetImage().Save ("XXX.png", System.Drawing.Imaging.ImageFormat.Png);

EDIT - for WPF try this:

public static void SaveClipboardImageToFile(string filePath)
{
    var image = Clipboard.GetImage();
    using (var fileStream = new FileStream(filePath, FileMode.Create))
    {
        BitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        encoder.Save(fileStream);
    }
}
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • I am assuming Clipboard.GetImage().Save(...) is Winform only? There is nothing like that in WPF, and I would really like to not reference any Winform calls. – Daniel Clark Jul 20 '11 at 23:38
  • Tried out your WPF code, and it's still producing a solid black image output. I should mention that I am copying the image from Internet Explorer 9 (example: http://images.findicons.com/files/icons/1035/human_o2/128/orange_folder.png, but no idea if that makes a different? – Daniel Clark Jul 21 '11 at 00:10
  • yes - that seems to make a difference... I tried it with firefox... when I download it it has transparency... when I copy it to clipboard, it looses transparency (i.e. pasted it to photoshop) – Yahia Jul 21 '11 at 00:13
  • Hrm, I just went and tried with Chrome and it came out with transperency too. A very strange one. A bug with IE9 image handling I suppose? – Daniel Clark Jul 21 '11 at 00:14
  • not really a bug... when an app copies something something to cliepboard it can do so in any format it chooses to... firefox and IE seem to copy into clipboard without transparency... – Yahia Jul 21 '11 at 00:16