locked
Getting ImageFormat from bitmap RRS feed

  • Question

  • User-1825738080 posted

     I'm trying to determine which extension I should use when saving an image to disk from the Bitmap object.

     

            private string GetExtension(Bitmap image)
    {
    if (image.RawFormat == ImageFormat.Bmp)
    return ".bmp";
    if (image.RawFormat == ImageFormat.Gif)
    return ".gif";
    if (image.RawFormat == ImageFormat.Png)
    return ".png";
    if (image.RawFormat == ImageFormat.Jpeg)
    return ".jpg";
    throw new FlexiException("Unsupported file format {0}", image.RawFormat.ToString());
    }

      In the debugger, it says the ImageFormat is Guid value

     How do I determine the Bitmap's format to save the file correctly?

    Monday, January 19, 2009 1:21 PM

Answers

  • User2130758966 posted

     See this snippet:

     

       public string Format
       {
         get 
         {
          ImageFormat bmpFormat = m_bmpRepresentation.RawFormat;
          string strFormat = "unidentified format";
          
    	  if (bmpFormat.Equals(ImageFormat.Bmp)) strFormat = "BMP";
    	  else if (bmpFormat.Equals(ImageFormat.Emf)) strFormat = "EMF";
    	  else if (bmpFormat.Equals(ImageFormat.Exif)) strFormat = "EXIF";
    	  else if (bmpFormat.Equals(ImageFormat.Gif)) strFormat = "GIF";
    	  else if (bmpFormat.Equals(ImageFormat.Icon)) strFormat = "Icon";
    	  else if (bmpFormat.Equals(ImageFormat.Jpeg)) strFormat = "JPEG";
    	  else if (bmpFormat.Equals(ImageFormat.MemoryBmp)) strFormat = "MemoryBMP";
    	  else if (bmpFormat.Equals(ImageFormat.Png)) strFormat = "PNG";
    	  else if (bmpFormat.Equals(ImageFormat.Tiff)) strFormat = "TIFF";
    	  else if (bmpFormat.Equals(ImageFormat.Wmf)) strFormat = "WMF";
          
          return strFormat;
         }
       }
    

      

    And this explanation:

     ....as the image format is defined as GUID and I therefore have to perform all comparisons with Equals....

    From this page:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 19, 2009 1:44 PM