Reputation: 615
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?
Upvotes: 4
Views: 2633
Reputation: 70369
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);
}
}
Upvotes: 3