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

时间:2016-03-13 13:48:08

标签: c# .net image file-extension

我试图找出没有扩展名的文件是否是图像,但似乎无法正确使用。我知道它肯定是一个图像,因为我可以用毫秒油漆打开它。这是我的代码

        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);
    }

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

2 个答案:

答案 0 :(得分:1)

如果您尝试比较标头中的字节序列,那么比较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循环来仅测试给定签名数组中的字节数。

实际上,使用秒表没有足够的差异可以担心。如果您有数千个文件需要处理,这可能只是重要的。

答案 1 :(得分:0)

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

希望有所帮助