【问题标题】:Detect if a file with no extension is an image检测没有扩展名的文件是否是图像
【发布时间】:2016-06-28 12:24:26
【问题描述】:

我试图找出没有扩展名的文件是否是图像,但似乎无法正确处理。我知道它绝对是一张图片,因为我可以在 ms paint 中打开它。这是我的代码

        private bool IsImage(Stream stream)
    {
        stream.Seek(0, SeekOrigin.Begin);

        List<string> jpg = new List<string> { "FF", "D8" };
        List<string> bmp = new List<string> { "42", "4D" };
        List<string> gif = new List<string> { "47", "49", "46" };
        List<string> png = new List<string> { "89", "50", "4E", "47", "0D", "0A", "1A", "0A" };
        List<List<string>> imgTypes = new List<List<string>> { jpg, bmp, gif, png };

        List<string> bytesIterated = new List<string>();

        for (int i = 0; i < 8; i++)
        {
            string bit = stream.ReadByte().ToString("X2");
            bytesIterated.Add(bit);

            bool isImage = imgTypes.Any(img => !img.Except(bytesIterated).Any());
            if (isImage)
            {
                textBox1.Text = "is image";
                return true;
            }
        }
        textBox1.Text = "is not image";
        return false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string filepath = @"C:\Users\William\Documents\drivers\2";
        MemoryStream mStrm = new MemoryStream(Encoding.UTF8.GetBytes(filepath));
        IsImage(mStrm);
    }

也忽略它在一个名为驱动程序的文件中,该文件不是驱动程序或任何东西

【问题讨论】:

标签: c# .net image file-extension


【解决方案1】:

如果您尝试比较标头中的字节序列,比较 byte[] 似乎比比较 strings 更好。

// simple class to associate a signature with a name
public class ImgHeader
{
    public readonly string Name;
    public readonly byte[] Header;

    public static readonly ImgHeader GIF89a = new ImgHeader("GIF89a", new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 });
    public static readonly ImgHeader GIF87a = new ImgHeader("GIF87a", new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 });
    public static readonly ImgHeader JPG = new ImgHeader("JPG", new byte[]{0xFF, 0xD8});
    public static readonly ImgHeader PNG = new ImgHeader("PNG", new byte[] {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A });

    private ImgHeader(string n, byte[] h)
    {
        this.Name = n;
        this.Header = h;
    }
}

然后,它们的集合(请注意,列表可能更长 BMP、TIFF 等):

List<ImgHeader> imgSigs = new List<ImgHeader>();

imgSigs.Add(ImgHeader.GIF87a);
imgSigs.Add(ImgHeader.GIF89a);
imgSigs.Add(ImgHeader.JPG);
imgSigs.Add(ImgHeader.PNG);

给定一个代表完整文件名的List&lt;string&gt;,迭代并比较标题字节:

foreach (string s in files)
{
    using (FileStream fs = new FileStream(s,FileMode.Open, FileAccess.Read))
    using (BinaryReader br = new BinaryReader(fs))
    { 
        //max header size
        byte[] hdr =  br.ReadBytes(8);

        foreach (ImgHeader sig in imgSigs)
        {
             // subset of bytes read for comparison
             byte[] testHdr = new byte[sig.Header.Length];
             Array.Copy(hdr, testHdr, sig.Header.Length);

             //if( CompareBytes(hdr, sig.Header))
             if (testHdr.SequenceEqual(sig.Header))
             { 
                Console.WriteLine("{0} is {1}", s, sig.Name);
                break;
             }
        }
    }
}

与其创建临时数组并复制以使用SequenceEqual,不如调用使用for n 循环仅测试给定签名数组中的字节数的比较器方法可能更快。


实际上,使用秒表并没有足够的差异来担心。仅当您有数千个文件要处理时才重要。

【讨论】:

    【解决方案2】:

    像这样使用 FileStream 代替 MemoryStream :

    private void button1_Click(object sender, EventArgs e)
        {
            string filepath = @"C:\Users\William\Documents\drivers\2";
            var mStrm = new FileStream(filepath , FileMode.Open, FileAccess.Read)
            IsImage(mStrm);
        }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2012-11-14
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多