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

后端 未结 16 2330
失恋的感觉
失恋的感觉 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:37

    This will loop through each file in the current directory and will output if any found files with JPG or JPEG extension are Jpeg images.

          foreach (FileInfo f in new DirectoryInfo(".").GetFiles())
            {
                if (f.Extension.ToUpperInvariant() == ".JPG"
                    || f.Extension.ToUpperInvariant() == ".JPEG")
                {
                    Image image = Image.FromFile(f.FullName);
    
                    if (image.RawFormat == ImageFormat.Jpeg)
                    {
                        Console.WriteLine(f.FullName + " is a Jpeg image");
                    }
                }
            }
    

提交回复
热议问题