110,533
社区成员
发帖
与我相关
我的任务
分享
public string CheckImage(string fileName)
{
FileStream fs = null;
BinaryReader reader = null;
Image image = null;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
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();
//255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
List<string> list = new List<string>() { "255216", "7173", "6677", "13780" };
if (list.Contains(fileClass))
{
image = Image.FromFile(fileName);
}
else
{
return "非图片文件,无法打开!";
}
}
catch (Exception ex)
{
return "图片打开失败,可能格式存在问题:" + ex.Message;
}
finally
{
if (image != null) image.Dispose();
reader.Close();
fs.Close();
}
return "success";
}