Reputation: 434
I am trying to essentially create a proxy image loader that can download and return an image. My code is very close, it is displaying an image however I believe some of the bytes just aren't coming out right:
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
Any help is greatly appreciated.
Upvotes: 1
Views: 1671
Reputation: 34846
Try this:
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
Note: You can also just point an ASP.NET Image server control to the URL of the image you want downloaded, like this:
<asp:Image id="img1" runat="server" ImageUrl="URL" />
Upvotes: 2