把图片下载过来到内存里,然后response出去

2010-08-28 10:50:27来源:西部e网作者:

先用WebRequest下载图片,再Response出去。
string imgUrl="http://community.csdn.net/images/CSDN_logo.GIF";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(imgUrl);
HttpWebResponse resResult = (HttpWebResponse) request.GetResponse();
Stream s = resResult.GetResponseStream();

Bitmap bmp=new Bitmap(s);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Gif);

Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
bmp.Dispose();
Response.End();

如果要判断不同的图片格式:
string imgType=imgUrl.Substring(imgUrl.LastIndexOf(".")+1).ToLower();
switch(imgType)
{
case "jpg":
imgType="image/jpeg";
break;
case "gif":
imgType="image/gif";
break;
//...........
default:
return;
}
关键词:ASP.NET

赞助商链接: