C#通过字节值判断图片格式

it2022-05-08  7

 

public byte[] GetContent() { byte[] content =null; if ( this.fileInfo != null && this.fileInfo.Exists) { using (Stream stream = this.fileInfo.OpenRead()) { content = new byte[stream.Length]; stream.Read(content, 0, this.content.Length); } } return content; }

 

public static string GetFileSuffix(byte[] fileData) { string result; if (fileData == null || fileData.Length < 10) { result = null; } else { if (fileData[0] == 71 && fileData[1] == 73 && fileData[2] == 70) { result = "GIF"; } else { if (fileData[1] == 80 && fileData[2] == 78 && fileData[3] == 71) { result = "PNG"; } else { if (fileData[6] == 74 && fileData[7] == 70 && fileData[8] == 73 && fileData[9] == 70) { result = "JPG"; } else { if (fileData[0] == 66 && fileData[1] == 77) { result = "BMP"; } else { result = null; } } } } } return result; }

 

 

 

转载于:https://www.cnblogs.com/jzblog/p/4738433.html


最新回复(0)