简体   繁体   中英

Detect if a file with no extension is an image

im trying to find out if a file with no extension is an image but cant seem to get it right. i know its definitely an image because i can open it in ms paint. here is my code anyway

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

also ignore that its in a file called drivers, the file is not a driver or anything

If you are trying to compare byte sequences in a header, it seems better to compare byte[] than string s.

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

Then, a collection of them (note that the list could be longer BMP, TIFF etc):

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

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

Given a List<string> representing the full file name, iterate and compare the header bytes:

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

Rather than creating a temp array and copying to use SequenceEqual it might be faster to call a comparer method which employs a for n loop to only test as many bytes as are in the given signature array.


Actually, using a stopwatch there is not enough difference to worry about. It might only matter if you have thousands of files to process.

use FileStream instead MemoryStream like this :

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

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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