Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know what image format I get from a stream?

I get a byte stream from some web service. This byte stream contains the binary data of an image and I'm using the method in C# below to convert it to an Image instance.

I need to know what kind of image I've got. Is it a simple bitmap (*.bmp) or a JPEG image (*.jpg) or a png image?

How can I find it out?

    public static Image byteArrayToImage( byte[] bmpBytes )     {         Image image = null;         using( MemoryStream stream = new MemoryStream( bmpBytes ) )         {             image = Image.FromStream( stream );         }          return image;     } 
like image 662
Yanshof Avatar asked Mar 06 '11 08:03

Yanshof


People also ask

What are the 3 common file type of an image file?

The PNG, JPEG, and GIF formats are most often used to display images on the Internet. Some of these graphic formats are listed and briefly described below, separated into the two main families of graphics: raster and vector.

What format are images saved?

JPEG stands for Joint Photographic Experts Group, and it's extension is widely written as . jpg. This most used image file format is used to store photos all over the world, and is generally a default file format for saving images.


2 Answers

You may checkout the Image.RawFormat property. So once you load the image from the stream you could test:

if (ImageFormat.Jpeg.Equals(image.RawFormat)) {     // JPEG } else if (ImageFormat.Png.Equals(image.RawFormat)) {     // PNG } else if (ImageFormat.Gif.Equals(image.RawFormat)) {     // GIF } ... etc 
like image 124
Darin Dimitrov Avatar answered Sep 19 '22 03:09

Darin Dimitrov


You can get the image type from the following code:

//get your image from bytaArrayToImage Image img = byteArrayToImage(new byte[] { });  //get the format/file type string ext = new ImageFormatConverter().ConvertToString(img.RawFormat); 
like image 36
Onsongo Moseti Avatar answered Sep 21 '22 03:09

Onsongo Moseti