Results 1 to 6 of 6

Thread: Load image from URL into Picturebox at runtime

  1. #1

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141

    Load image from URL into Picturebox at runtime

    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
    Code:
    ' 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 using
    Code:
    picProductImage.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.

  2. #2
    Member
    Join Date
    Oct 2000
    Location
    Philadelphia
    Posts
    47
    Did you try using Ctype() function?
    U S A
    Visual Studio .NET
    Windows XP

  3. #3

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141
    Well I changed line 6 of the code I posted to
    Code:
    Dim strImage As System.IO.Stream = CType(oXMLHTTP.responseStream, System.IO.Stream)
    but the message is still the same
    An unhandled exception of type 'System.InvalidCastException' occurred in macXchange Admin.exe

    Additional information: Specified cast is not valid.

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    use webclient instead or httpclient

    dim wc as new WebClient()
    wc.downloaddata()
    \m/\m/

  5. #5
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Code:
    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)

  6. #6

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width