Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检测没有扩展名的文件是否为图像_C#_.net_Image_File Extension - Fatal编程技术网

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

C# 检测没有扩展名的文件是否为图像,c#,.net,image,file-extension,C#,.net,Image,File Extension,我试图找出一个没有扩展名的文件是否是一个图像,但似乎无法正确获取它。我知道这绝对是一个图像,因为我可以在ms paint中打开它。这是我的代码 private bool IsImage(Stream stream) { stream.Seek(0, SeekOrigin.Begin); List<string> jpg = new List<string> { "FF", "D8" }; List&

我试图找出一个没有扩展名的文件是否是一个图像,但似乎无法正确获取它。我知道这绝对是一个图像,因为我可以在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);
    }
private bool IsImage(流)
{
stream.Seek(0,SeekOrigin.Begin);
List jpg=新列表{“FF”,“D8”};
列表bmp=新列表{“42”,“4D”};
List gif=新列表{“47”、“49”、“46”};
列表png=新列表{“89”、“50”、“4E”、“47”、“0D”、“0A”、“1A”、“0A”};
List imgTypes=新列表{jpg,bmp,gif,png};
List bytesIterated=新列表();
对于(int i=0;i<8;i++)
{
字符串位=stream.ReadByte().ToString(“X2”);
字节迭代。添加(位);
bool isImage=imgTypes.Any(img=>!img.Except(bytesiiterated.Any());
如果(isImage)
{
textBox1.Text=“是图像”;
返回true;
}
}
textBox1.Text=“不是图像”;
返回false;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
字符串文件路径=@“C:\Users\William\Documents\drivers\2”;
MemoryStream mStrm=新的MemoryStream(Encoding.UTF8.GetBytes(filepath));
IsImage(mStrm);
}

另请忽略它在一个名为drivers的文件中,该文件不是驱动程序或任何东西,请使用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);
    }

希望对您有所帮助

如果您试图比较头中的字节序列,比较
字节[]
似乎比比较
字符串
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;
    }
}
然后是它们的集合(请注意,列表可能更长,如BMP、TIFF等):

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



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

检查这里:这看起来很有希望,我会看一看,谢谢您收到的错误消息是什么?
List<ImgHeader> imgSigs = new List<ImgHeader>();

imgSigs.Add(ImgHeader.GIF87a);
imgSigs.Add(ImgHeader.GIF89a);
imgSigs.Add(ImgHeader.JPG);
imgSigs.Add(ImgHeader.PNG);
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;
             }
        }
    }
}