繁体   English   中英

检测没有扩展名的文件是否是图像

[英]Detect if a file with no extension is an image

我试图找出没有扩展名的文件是否是图像,但似乎无法正确处理。 我知道它绝对是一个图像,因为我可以在 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);
    }

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

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

// 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<string> ,迭代并比较标头字节:

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循环仅测试给定签名数组中的字节数。


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

像这样使用 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);
    }

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM