如何使用 asp.net 从 URL 下载和显示图像
例如,我有一个页面 ShowImage.aspx 当我调用此页面时,它应该显示来自外部 URL https://appharbor.com/assets/images/stackoverflow-logo.png的图像(来自响应流)
带有 Response.ContentType = "图像/png"
自行解决
protected void Page_Load(object sender, EventArgs e)
{
WebRequest req = WebRequest.Create("https://appharbor.com/assets/images/stackoverflow-logo.png");
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
Byte[] buffer = null;
Response.Clear();
Response.ContentType = "image/png";
Response.AddHeader("Content-Length", response.ContentLength.ToString());
int bytesRead = 0;
do
{
buffer = new Byte[4096];
bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
Response.OutputStream.Write(buffer,0,bytesRead);
}
} while (bytesRead > 0);
Response.End();
}