简体   繁体   中英

How to get the image format of a BitmapImage?

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?

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);
}

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, eg PNG when you use PngBitmapEncoder .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM