1

我正在尝试创建一个可以下载和返回图像的代理图像加载器。我的代码非常接近,它正在显示图像,但是我相信某些字节并没有正确显示:

Dim myRequest As WebRequest = WebRequest.Create(URL)
Dim myResponse As WebResponse = myRequest.GetResponse()
Response.ContentType = "image/jpeg"
Response.Clear()
Response.BufferOutput = True
Dim strm As Stream = myResponse.GetResponseStream()
Dim buffer As Byte() = New Byte(4095) {}
Dim byteSeq As Integer = strm.Read(buffer, 0, 4096)
Do While byteSeq > 0
    Response.OutputStream.Write(buffer, 0, 4096)
    byteSeq = strm.Read(buffer, 0, 4096)
    Response.Flush()
Loop

任何帮助是极大的赞赏。

4

1 回答 1

2

试试这个:

Dim theRequest As WebRequest = WebRequest.Create(URL)
Dim theResponse As WebResponse = theRequest.GetResponse()
Dim theStream As Stream = theResponse.GetResponseStream()
Dim theImage As System.Drawing.Image = System.Drawing.Image.FromStream(theStream)

Using theMemoryStream As New MemoryStream()
    theImage.Save(theMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
    theMemoryStream.WriteTo(Response.OutputStream)
End Using

注意:您也可以只将 ASP.NET Image 服务器控件指向要下载的图像的 URL,如下所示:

<asp:Image id="img1" runat="server" ImageUrl="URL" />
于 2013-09-11T19:37:23.973 回答