Results 1 to 19 of 19

Thread: Display image from internet in a Picturebox

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Display image from internet in a Picturebox

    VB Code:
    1. 'WEB CLIENT IS NEEDED TO DO THE DOWNLOAD
    2.         Dim MyWebClient As New System.Net.WebClient
    3.  
    4.         'BYTE ARRAY HOLDS THE DATA
    5.         Dim ImageInBytes() As Byte = MyWebClient.DownloadData(TextBox1.Text)
    6.  
    7.         'CREATE A MEMORY STREAM USING THE BYTES
    8.         Dim ImageStream As New IO.MemoryStream(ImageInBytes)
    9.  
    10.         'CREATE A BITMAP FROM THE MEMORY STREAM
    11.         PictureBox1.Image = New System.Drawing.Bitmap(ImageStream)
    12.  
    13.         'AS U SEE, NO FILE NEEDS TO BE WRITTEN TO THE HARD DRIVE, ITS ALL DONE IN MEMORY

    Or to merge all that into 1 line of code

    VB Code:
    1. PictureBox1.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData(TextBox1.Text)))

    Assumes there is a textbox called textbox1 and a picturebox called picturebox1 on a form. Textbox1.text should be the URL of the picture.

    This shows how you can grab a picture off the web in .NET 1.1 and display it in a picturebox without actually having to save the image to file first. It uses a memory stream so no data has to be physically written to the drive to then be displayed.

    The example uses an image, and a picturebox, but you can see how this could be applied to other file types and used with other classes that can be created via a stream or bytes.
    Last edited by kleinma; Mar 19th, 2006 at 06:57 PM.

  2. #2
    Member
    Join Date
    Mar 2005
    Posts
    56

    Re: Display image from internet in a Picturebox

    thanks, it very useful..

    but, do you know how to implement in the smart device?

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Display image from internet in a Picturebox

    you mean a device running the compact framework?

  4. #4
    New Member
    Join Date
    Mar 2006
    Posts
    7

    Re: Display image from internet in a Picturebox

    Kleinma, you are a life saver! Thank you!

  5. #5
    Member
    Join Date
    Mar 2005
    Posts
    56

    Re: Display image from internet in a Picturebox

    Quote Originally Posted by kleinma
    you mean a device running the compact framework?
    yes,
    I write the same code in the pocketpc, and it show no reference of System.Net.WebClient...that mean i can not use System.Net.WebClient class..

  6. #6
    New Member
    Join Date
    Apr 2006
    Posts
    1

    Re: Display image from internet in a Picturebox

    Hi! the code is really useful. However, im having a problem here. In my project, the image from the internet is changed frequently. My project image too, have to be changed. However, whenever the image changes, the url change. Is there a way i can solve this? I tried using substring but to no result. Thanks alot!

  7. #7
    New Member Billcat's Avatar
    Join Date
    Apr 2006
    Posts
    2

    Re: Display image from internet in a Picturebox

    the picurebox can show local Gif format image well in VB2005

    when i ran this code segment in VB2005, it worked fine if i typed a valid url like "http:// ... /image1.jpg" in textbox,but if i typed a valid url like "http:// ... /image1.gif",a wrong messagebox show:"A generic error occurred in GDI+. ",and the errorcode is -2147467259.

    i don't kown how to resolve this problem, please help..

  8. #8
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Display image from internet in a Picturebox

    the WebClient method has always worked well for me over the years on gifs as well as jpgs, but as an alternative if you are having trouble, you could try the HttpWebRequest / HttpWebResponse method.
    like this ...
    VB Code:
    1. [COLOR=Blue]Dim[/COLOR] req [COLOR=Blue]As[/COLOR] Net.HttpWebRequest = [COLOR=Blue]DirectCast[/COLOR](Net.HttpWebRequest.Create([COLOR=Purple]"http://www.google.co.uk/intl/en_uk/images/logo.gif"[/COLOR]), Net.HttpWebRequest)
    2.         [COLOR=Blue]Dim[/COLOR] res [COLOR=Blue]As[/COLOR] Net.HttpWebResponse = [COLOR=Blue]DirectCast[/COLOR](req.GetResponse, Net.HttpWebResponse)
    3.  
    4.         [COLOR=Blue]Dim[/COLOR] img [COLOR=Blue]As[/COLOR] Image = Image.FromStream(res.GetResponseStream)
    5.  
    6.         res.Close()
    7.         '/// StretchImage to make image the same size as picturebox
    8.         '/// AutoSize to make picturebox same size as image
    9.         PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    10.         PictureBox1.Image = img
    that's my personal preffered way of downloading images anyway
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  9. #9
    New Member Billcat's Avatar
    Join Date
    Apr 2006
    Posts
    2

    Re: Display image from internet in a Picturebox

    [VB2005] two code segments can display static gif image from internet good, but display animatic gif image form internet ariseing a mistake

  10. #10
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Display image from internet in a Picturebox

    You could also modify this code just a tad to make it a bit more versatile by allowing the option to save the file to disk upon displaying it. I turned it into a function by adjusting a few simple things, like so:

    VB Code:
    1. Public Function webDownloadImage(ByVal Url As String, Optional ByVal saveFile As Boolean = False, Optional ByVal location As String = "C:\") As Image
    2.  
    3.         Dim webClient As New System.Net.WebClient
    4.         Dim bytes() As Byte = webClient.DownloadData(Url)
    5.         Dim stream As New IO.MemoryStream(bytes)
    6.  
    7.         If saveFile Then My.Computer.FileSystem.WriteAllBytes(location, bytes, False)
    8.  
    9.         Return New System.Drawing.Bitmap(stream)
    10.  
    11.     End Function

    This way, you can use it as:

    VB Code:
    1. PictureBox1.Image = webDownloadImage("http://www.somewebsite.com/image.jpg", True, "C:\temp.jpg")

    This will allow you to not only use it as a direct source for an image, but allow you to save the image to your hard drive if you should so choose. Good code, in any case. VERY useful ;D

  11. #11
    New Member
    Join Date
    Feb 2007
    Location
    Madrid, Spain
    Posts
    4

    Re: Display image from internet in a Picturebox

    (my english is not to good)

    hi, finally I found a solution for this .... I don't know if there is a solution anywhere in the forum, but I found that.

    I work with Vb .net compact framework, so many properties of .net framework doesn't works

    now, this is what I used

    VB Code:
    1. Dim req As Net.HttpWebRequest = DirectCast(Net.HttpWebRequest.Create(img_url), Net.HttpWebRequest)
    2. Dim res As Net.HttpWebResponse = DirectCast(req.GetResponse, Net.HttpWebResponse)
    3. Dim img As Image = New System.Drawing.Bitmap(res.GetResponseStream)
    4. res.Close()
    5. PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    6. PictureBox1.Image = img

    where img_url is the url of the image I want to use, and is a string

    I tested it on my PocketPC and it works

    thanks

  12. #12
    New Member
    Join Date
    Jun 2007
    Posts
    1

    Re: Display image from internet in a Picturebox

    Quote Originally Posted by egomezpe
    (my english is not to good)

    hi, finally I found a solution for this .... I don't know if there is a solution anywhere in the forum, but I found that.

    I work with Vb .net compact framework, so many properties of .net framework doesn't works

    now, this is what I used

    VB Code:
    1. Dim req As Net.HttpWebRequest = DirectCast(Net.HttpWebRequest.Create(img_url), Net.HttpWebRequest)
    2. Dim res As Net.HttpWebResponse = DirectCast(req.GetResponse, Net.HttpWebResponse)
    3. Dim img As Image = New System.Drawing.Bitmap(res.GetResponseStream)
    4. res.Close()
    5. PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    6. PictureBox1.Image = img

    where img_url is the url of the image I want to use, and is a string

    I tested it on my PocketPC and it works

    thanks

    hi,

    im trying to load an image on a mobile device, the above code works fine; however i need to pass in some credentials as the image is located with a protected area of the site and i dont know how to do that.

    thanks

  13. #13

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Display image from internet in a Picturebox

    Quote Originally Posted by sapience
    hi,

    im trying to load an image on a mobile device, the above code works fine; however i need to pass in some credentials as the image is located with a protected area of the site and i dont know how to do that.

    thanks
    try this:

    Code:
            Dim req As Net.HttpWebRequest = Net.HttpWebRequest.Create(img_url)
            req.Credentials = New Net.NetworkCredential("usernamehere", "passwordhere")
            Dim res As Net.HttpWebResponse = DirectCast(req.GetResponse, Net.HttpWebResponse)
            Dim img As Image = New System.Drawing.Bitmap(res.GetResponseStream)
            res.Close()
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            PictureBox1.Image = img

  14. #14
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    382

    Re: Display image from internet in a Picturebox

    I'm trying to load an image into a picturebox. The image is on a webpage but I cannot access the direct location of the image. I want to load the website, and have whatever image appears on that website be loaded into a picture box. The image is in .jpg. I cannot simply load the URL the image is at, as the way the webpage is setup I cannot link to the image. The image is generated from a script.
    Last edited by CatchItBaby; Aug 20th, 2010 at 02:15 AM.

  15. #15

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Display image from internet in a Picturebox

    is it a captcha?

  16. #16
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    382

    Re: Display image from internet in a Picturebox

    no..

    It Was Avtar image ie from social netowork site

  17. #17
    New Member
    Join Date
    Feb 2011
    Posts
    1

    Re: Display image from internet in a Picturebox

    How I can display a picture from a url in excel userform?

    Can I use the same code?

    I tried but it is giving me some error.

  18. #18
    New Member
    Join Date
    Dec 2011
    Posts
    1

    Re: Display image from internet in a Picturebox

    Quote Originally Posted by kleinma View Post
    try this:

    Code:
            Dim req As Net.HttpWebRequest = Net.HttpWebRequest.Create(img_url)
            req.Credentials = New Net.NetworkCredential("usernamehere", "passwordhere")
            Dim res As Net.HttpWebResponse = DirectCast(req.GetResponse, Net.HttpWebResponse)
            Dim img As Image = New System.Drawing.Bitmap(res.GetResponseStream)
            res.Close()
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            PictureBox1.Image = img
    hi, i had try this with my own code for my wm6
    Code:
    Dim img Image = New.System.Drawing.Bitmap("C:\users\user\Desktop\123.bmp")
    PictureBox1.Image = img
    and i get this error: DirectoryNotFoundExxception was unhandled
    could not find a part of the 'C:\users\user\Desktop\123.bmp'.

    i double check the picture "123" location, it is correct, but i still get the error code.
    i just want to load the picture name "123" to my picturebox in my window mobile 6 professional form.
    please help me.

  19. #19
    New Member
    Join Date
    Jun 2012
    Posts
    1

    Re: Display image from internet in a Picturebox

    Sorry to bring this post back from the dead but I just wanted to add,
    in some cases you might get a 403 response from the server so you might want to add to the headers of your webclient object the user-agent like this:

    objWebClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705")

    That way you save some trouble.

    Sorry again for the zombie post.

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