Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,PDF]

public static FileExtension CheckFileExtension(string fileName)
  {
   if (!File.Exists(fileName))
   {
    return FileExtension.VALIDFILE;
   }
   FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
   System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
   string fileType = string.Empty;
   FileExtension extension = FileExtension.VALIDFILE;
   try
   {
    byte data = br.ReadByte();
    fileType += data.ToString();
    data = br.ReadByte();
    fileType += data.ToString();
    extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
    if (extension.ToString().Equals(fileType))
    {
     extension = FileExtension.VALIDFILE;
    }
   }
   catch
   {
    extension = FileExtension.VALIDFILE;
   }
   finally
   {
    if (fs != null)
    {
     fs.Close();
     br.Close();
    }
   }

   return extension;
  }
  public enum FileExtension
  {
   JPEG = 255216,
   TIF = 7373,
   GIF = 7173,
   BMP = 6677,
   PNG = 13780,
   PDF = 3780,
   VALIDFILE = 9999999
  }

你可能感兴趣的:(Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,PDF])