Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Get an Image from the Web

I have the following link
http://insight.coupons.com/OfferServices/GetImageCache.aspx?msk=y&cid=SEEEUSRI

That is linked to an image how do I download this to my computer

This is the code I found but it give me the Error "Invalid Parameter"

Dim b() As Byte = GetImageFromURL("http://insight.coupons.com/OfferServices/GetImageCache.aspx?msk=y&cid=SEEEUSRI")

Dim st As System.IO.MemoryStream = New System.IO.MemoryStream(b)
Dim img As System.Drawing.Bitmap = New System.Drawing.Bitmap(st)

Function GetImageFromURL(ByVal url As String) As Byte()
Dim wr As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)

Dim wresponse As System.Net.HttpWebResponse = DirectCast(wr.GetResponse, System.Net.HttpWebResponse)

Dim responseStream As System.IO.Stream = wresponse.GetResponseStream

Dim br As System.IO.BinaryReader = New System.IO.BinaryReader(responseStream)

Dim bytesize As Long = wresponse.ContentLength

Return br.ReadBytes(bytesize)

End Function

Any help would be great
[1136 byte] By [sremiger] at [2007-11-11 10:21:10]
# 1 Re: Get an Image from the Web
The problem is that GetImageFromURL is returning an empty array. When I try it with http://www.google.com/images/logo_sm.gif , the code runs without error.

Incidentally, if you're using .NET 2.0, GetImageFromURL may be simplified to:

Function GetImageFromURL(ByVal url As String) As Byte()
Dim wc As New System.Net.WebClient
Return wc.DownloadData(url)
End Function
Phil Weber at 2007-11-11 23:12:00 >
# 2 Re: Get an Image from the Web
The URL I pasted in is valid in the sense that what it returns is an image the difference between your URL and the above is that yours links directly to an image and mine indirectly based on the query string params returns an image.
sremiger at 2007-11-11 23:13:00 >