读取每个文件的头两个字节,
byte[0].ToString()+byte[1].ToString()的值
255216:jpg,7173:gif,6677:bmp,13780:png
private bool IsPicture(string filePath)//filePath是文件的完整路径
{
try
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fs);
string fileClass;
byte buffer;
byte[] b=new byte[2];
buffer = reader.ReadByte();
b[0] = buffer;
fileClass = buffer.ToString();
buffer = reader.ReadByte();
b[1]=buffer;
fileClass += buffer.ToString();
reader.Close();
fs.Close();
if (fileClass == "255216 ")//255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}

该代码示例展示了如何使用C#通过读取文件头两个字节来判断文件是否为图片(JPEG, GIF, BMP, PNG)。通过对字节进行组合并比较特定的标识符,如'255216'对应于JPEG,'7173'对应于GIF,'6677'对应于BMP,'13780'对应于PNG,来识别图片类型。"
129835490,17929155,Python编程:程序流程控制详解,"['Python', '数据分析', '数据挖掘']
722

被折叠的 条评论
为什么被折叠?



