判断文件是否是图片

  1. private bool IsPicture(string filePath)//filePath是文件的完整路径  
  2.         { 
  3.             try 
  4.             { 
  5.                 FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); 
  6.                 BinaryReader reader = new BinaryReader(fs); 
  7.                 string fileClass; 
  8.                 byte buffer; 
  9.                 byte[] b=new byte[2]; 
  10.                 buffer = reader.ReadByte(); 
  11.                 b[0] = buffer; 
  12.                 fileClass = buffer.ToString(); 
  13.                 buffer = reader.ReadByte(); 
  14.                 b[1]=buffer; 
  15.                 fileClass += buffer.ToString(); 
  16.  
  17.  
  18.                 reader.Close(); 
  19.                 fs.Close(); 
  20.                 if (fileClass == "255216 ")//255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar  
  21.                 { 
  22.                     return true
  23.                 } 
  24.                 else 
  25.                 { 
  26.                     return false
  27.                 } 
  28.             } 
  29.             catch 
  30.             { 
  31.                 return false
  32.             } 
  33.         } 
posted @ 2012-11-09 10:40  go on coding  阅读(1568)  评论(0编辑  收藏  举报
悠季瑜伽会馆