判断文件是否是图片

  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 @   go on coding  阅读(1575)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
悠季瑜伽会馆
点击右上角即可分享
微信分享提示