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