PDA

Click to See Complete Forum and Search --> : Load image from URL into Picturebox at runtime


mralston
Dec 6th, 2002, 05:50 AM
I want to load an image into a PictureBox from a URL at runtime. Any ideas how?

I was attempting to do it like this' Get image
Dim oXMLHTTP As New MSXML2.XMLHTTP()
oXMLHTTP.open("GET", "http://www.site.com/image.jpg")
oXMLHTTP.send()

Dim strImage As System.IO.Stream = oXMLHTTP.responseStream

oXMLHTTP = Nothing

' Load image into Picture Box
picProductImage.Image = Image.FromStream(strImage)but it told me that the cast (data type?) of the responsestream and image objects didn't match. I also tried usingpicProductImage.Image = Image.FromFile("http://www.site.com/image.jpg")Only to be told that the FromFile method didn't support URLs.

So I'm stuck.

Emerican
Dec 6th, 2002, 06:53 AM
Did you try using Ctype() function?

mralston
Dec 6th, 2002, 07:08 AM
Well I changed line 6 of the code I posted toDim strImage As System.IO.Stream = CType(oXMLHTTP.responseStream, System.IO.Stream) but the message is still the sameAn unhandled exception of type 'System.InvalidCastException' occurred in macXchange Admin.exe

Additional information: Specified cast is not valid.

PT Exorcist
Dec 6th, 2002, 07:12 AM
use webclient instead or httpclient

dim wc as new WebClient()
wc.downloaddata()

Athley
Dec 6th, 2002, 08:00 AM
Dim url As String = "http://www.site.com/image.jpg"
Dim wreq As System.Net.HttpWebRequest = System.Net.WebRequest.Create(url)
Dim wres As System.Net.HttpWebResponse = wreq.GetResponse
PictureBox1.Image = System.Drawing.Image.FromStream(wres.GetResponseStream)

mralston
Dec 6th, 2002, 05:19 PM
System.Net Thank you! I was sure there must be some namespace with stuff to do what I wanted, but I couldn't find it. I was looking through System.Web but couldn't find much useful. Using the XML object was a last ditch effort anyway. :)

I feel rather silly now for not spotting that namespace!