从网络上下载图片信息时经常会遇到这样的URL
http://c.csdnimg.cn/jifen/images/xunzhang/xunzhang/chizhiyiheng.png可以很顺利的得到图片的路径及图片名称
如果是这样的URL
http://letter5.vip.com/img/7D08DA0907A3DBDF57A08A7A384AB7EED91D8587
我们就无法得到其名称,只能通过图片文件数据来得到其类型
Stream stream=response.Content.ReadAsStreamAsync().Result
byte[] b=new byte[4];
stream.Read(b,0,b.Length);
string type=ToHexString(b).ToUpper(); //转大写
string hexType=string.Empty;
if (type.contains("FFD8FF")) {
hexType="jpg";
} else if (type.contains("89504E47")) {
hexType= "png";
} else if (type.contains("47494638")) {
hexType= "gif";
} else if (type.contains("49492A00")) {
hexType= "tif";
} else if (type.contains("424D")) {
hexType= "bmp";
}
public static string ToHexString(byte[] bytes
{
string hexString =string.Empty;
StringBuilder sb=new StringBuilder();
for(int i=0;i<bytes.Length;i++)
{
sb.Append(bytes[i].ToString("X2"));
}
hexString =sb.ToString();
return hexString;
}