Find image format using Bitmap object in C#

后端 未结 11 2520
半阙折子戏
半阙折子戏 2020-11-29 19:50

I am loading the binary bytes of the image file hard drive and loading it into a Bitmap object. How do i find the image type[JPEG, PNG, BMP etc] from the Bitmap object?

11条回答
  •  佛祖请我去吃肉
    2020-11-29 20:29

    If you want to know the format of an image, you can load the file with the Image class, and check its RawFormat property:

    using(Image img = Image.FromFile(@"C:\path\to\img.jpg"))
    {
        if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
        {
          // ...
        }
    }
    

提交回复
热议问题