Load a web image in VB.net

Dear Sirs,

Is there anyway of retrieving an image from a URL and displaying it in a picturebox?
watersidedesignsAsked:
Who is Participating?
 
S-TwilleyConnect With a Mentor Commented:
You have a few different options really...

Either use a WebBrowser control as a replacement picturebox, and use the Navigate2 Method to point it to the URL of the picture...

or as karlossos was directing at, downloading the image and displaying it... there's probably a proper way of doing it but you can use the WebClient class to download a file, and you could open that file into the picturebox, or download the data into an array of bytes and somehow transfer that into the picturebox (not at a machine with vb.net to try this out)... if i remember...

Imports System.Net

==========

Sub X()
    Dim wc as New WebClient
    Dim arrData() as Byte = wc.DownloadData("http://blah.com/ex.gif")  'something like this

    ' as for getting the bytes into the picturebox im not sure, i'll get back to you on that if no one else does
End Sub

the other method

Sub Y()
    Dim wc as New WebClient
    wc.DownloadFile("http://blah.com/ex.gif","c:\test.gif")
    Picturebox1.Image = New Bitmap("C:\test.gif")
End Sub

============

A word of warning, the webclient class, and the downloadXXXX methods will cause the current  thread to lock until the download is complete, so either do it on a seperate thread or use the WebRequest/WebResponse classes which take a bit more programming
0
 
karlossosCommented:
I would probably use a socket to connect to the web page.
Then pull the image from the web site to a temporary location.
then direct the picture box there.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnetsocketssocketclasstopic.asp

Here is some info on using sockets.

You will want to use the connect method to get the web site.

cheers.
0
 
armoghanCommented:
Three methods to do it apart from the above mentioned

1. http://weblogs.asp.net/psteele/articles/7533.aspx
2. http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=32290 . File stream can be URL as well
3. Use IE control instead of PictureBox. Though it would be heavy but it can easily have an Image from a URL
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
S-TwilleyCommented:
Ahh sorry armoghan... was a bit slow on the typing there... sorry to step on your toes
0
 
armoghanCommented:
NP
It happens all the time :)

For that I used to wish that EE shoudl work like IM :)
0
 
S-TwilleyCommented:
I usually type out my response, copy it to the clipboard, refresh the page to check no one has posted, then paste it back in and submit... but Im just getting lazy nowadays
0
 
watersidedesignsAuthor Commented:
Hi Guys,

Thanks for all the responses.  As it happen I need to wait for the download to complete before I can continue so I have tried the webclient download mentioned by S-Twilley.  However, whilst there is a pause as if something is happening, no image is downloaded.

Any Ideas?
0
 
watersidedesignsAuthor Commented:
Ignore my last comment, my fault :)

Thanks for all your help
0
 
S-TwilleyCommented:
well there's always going to be some sort of delay while the image is downloading... but maybe before you start the download, you load a local image into the picture box... maybe a "waiting for download" image... and once the download is complete, override the waiting image with the downloaded one
0
 
S-TwilleyCommented:
       Dim wc As New Net.WebClient

        Dim x As New Bitmap(wc.OpenRead("http://www.experts-exchange.com/images/expertAwards2004Button.gif"))
        PictureBox1.Image = x

=========

goto vb.net... here's what I was getting at about loading the data straight into a bitmap.
0
 
S-TwilleyCommented:
You really should have split the points... or even awarded  them all to the other two people... although I did put some sort of code in my comment, the others did post up valid links to solutions... I'm sure they're not going to come around and bust your front door down... but least remember it for next time :P
0
 
armoghanCommented:
:)
0
 
watersidedesignsAuthor Commented:
Sorry guys,  will remember next time :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.