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

 

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; }

 

 

 

posted @ 2015-08-18 09:37  寒山石  阅读(1972)  评论(0)    收藏  举报