C# How can I test a file is a jpeg?

后端 未结 16 2328
失恋的感觉
失恋的感觉 2020-11-27 03:28

Using C# how can I test a file is a jpeg? Should I check for a .jpg extension?

Thanks

16条回答
  •  [愿得一人]
    2020-11-27 03:49

    You could try loading the file into an Image and then check the format

    Image img = Image.FromFile(filePath);
    bool isBitmap = img.RawFormat.Equals(ImageFormat.Jpeg);
    

    Alternatively you could open the file and check the header to get the type

提交回复
热议问题