判断图片byte[]是否合法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
bool IsValidPic(byte[] bts)
{
if (bts == null || bts.Length < 4)
{
return false;
}
string fileClass = "";
int len = bts.Length;
try
{
fileClass = bts[0].ToString();
fileClass += bts[1].ToString();
fileClass = fileClass.Trim();
if (fileClass == "7173" || fileClass == "13780")//7173是gif;,13780是PNG;
{
return true;
}
else // JpgOrJpeg
{
byte[] Jpg = new byte[4];
Jpg[0] = 0xff;
Jpg[1] = 0xd8;
Jpg[2] = 0xff;
Jpg[3] = 0xd9;
if (bts[0] == Jpg[0] && bts[1] == Jpg[1]
&& bts[len - 2] == Jpg[2] && bts[len - 1] == Jpg[3])
{
return true;
}
else
{
return false;
}
}
}
catch (Exception ex)
{
//Log.WriteLog(e.ToString() + " fileclass = " + fileClass);
WriteLog.LogException(ex);
return false;
}
}