Results 1 to 19 of 19

Thread: Display image from internet in a Picturebox

Threaded View

  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.

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