判断图片byte[]是否合法

判断图片byte[]是否合法

ContractedBlock.gif ExpandedBlockStart.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 ;
}
}

 

posted on 2010-08-30 13:26 RockyLOMO 阅读(...) 评论(...) 编辑 收藏

你可能感兴趣的:(判断图片byte[]是否合法)