How to save an image in it's original format?
Posted
by Patrick Klug
on Stack Overflow
See other posts from Stack Overflow
or by Patrick Klug
Published on 2010-05-10T06:48:37Z
Indexed on
2010/05/10
6:54 UTC
Read the original article
Hit count: 227
I am trying to figure out how I can get the original format of an image so that I can store it with the same encoding.
It seems that the only way to save an Image is by using a BitmapEncoder but I don't know how I can get the correct format from the image.
Example:
Clipboard.GetImage() returns a InteropBitmap which doesn't seem to contain any information about the original format.
I also tried using an Extension method:
public static void Save(this BitmapImage image, System.IO.Stream stream)
{
var decoder = BitmapDecoder.Create(image.StreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
var encoder = BitmapEncoder.Create(decoder.CodecInfo.ContainerFormat);
foreach (var frame in decoder.Frames)
{
encoder.Frames.Add(BitmapFrame.Create(decoder.Frames[0]));
}
encoder.Save(stream);
}
but the problem is that a) the ImageSource is not always a BitmapImage (Clipboard.GetImage()) for example and b) the image.StreamSource can be null in some cases (seems to be when the image is loaded via a relative Uri)
© Stack Overflow or respective owner