public static string GetImageExtension(byte[] bytes)
{
if (bytes.Length < 12) return "unknown";
if (bytes[0] == 0xFF && bytes[1] == 0xD8) return "jpg";
if (bytes[0] == 0x89 && bytes[1] == 0x50) return "png";
if (bytes[0] == 0x47 && bytes[1] == 0x49) return "gif";
if (bytes[0] == 0x52 && bytes[1] == 0x49 && bytes[8] == 0x57 && bytes[9] == 0x45) return "webp";
return "unknown";
}
HttpClient client = new HttpClient(); //正式环境不这样使用
var bytes = client.GetByteArrayAsync(url).Result;
string ext = Path.GetExtension(url); //url以扩展名结尾的,先用库函数判断
if (string.IsNullOrEmpty(ext))
{
ext= GetImageExtension(bytes);
}
评论 (0)
暂无评论,快来发表第一条评论吧!