Reputation: 450
I have a WPF application with an Image element whose source is bound to a property of type BitmapImage. I need to save this image into our database along with it's format (jpg, tif, basically I'm trying to use the ImageFormat class). How can I get the format of the BitmapImage?
Upvotes: 0
Views: 1585
Reputation: 31
The Image is a control that displays a BitmapImage. There is no meaning to save the Image. You want to save the bitmapImage (the image.Source). Also you should not care of the original encoding/decoding of the bitmapImage. This has to do with the data compression in the file on the disk. Now the thing is in memory as BitmapImage with no compression. You have to decide of the encoding method of your local file. For .jpg you have to run the following code :
BitmapEncoder bme = new JpegBitmapEncoder();
bme.Frames.Add(BitmapFrame.Create(image.Source));
using (var filestream = new System.IO.FileStream("temp.jpg", System.IO.FileMode.Create))
{
bme.Save(fileStream);
}
Upvotes: 1
Reputation: 128061
How can I get the format of the BitmapImage?
You can't, because it does not have a fixed format.
You may use any of the classed derived from BitmapEncoder
to encode a BitmapSource. The format is then determined by the BitmapEncoder that is actually used, e.g. PNG
when you use PngBitmapEncoder
.
Upvotes: 0