Results 1 to 3 of 3

Thread: help - PictureBox from Web(URL) [Smoothly]

  1. #1

    Thread Starter
    Member intellilogic's Avatar
    Join Date
    Oct 2010
    Location
    Tyson's Corner, VA
    Posts
    39

    Question help - PictureBox from Web(URL) [Smoothly]

    I am using this code I got when I googled. It locks up the form for 4-10 full seconds! Very strange. I have a VERY FAST comp/connection. I am looking for better code that runs smoothly. I'd also like if it fails to load something like error.jpg or something, not just staying blank. I don't know where to throw this in the code. I have only loaded 1 PictureBox on the interface, but in the end, I want to load 6-10 on one form at one time.

    Code:
    Imports System.IO
    Imports System.Net
    Imports System.Text
    
        Public Sub LoadPics()
            LoadWebImageToPictureBox(picCrop1, "http://www.sample.com/image.jpg")
        End Sub
    
        Public Function LoadWebImageToPictureBox(ByVal pb _
          As PictureBox, ByVal ImageURL As String) As Boolean
    
            Dim objImage As MemoryStream
            Dim objwebClient As WebClient
            Dim sURL As String = Trim(ImageURL)
            Dim bAns As Boolean
    
            Try
                If Not sURL.ToLower().StartsWith("http://") _
                     Then sURL = "http://" & sURL
                objwebClient = New WebClient()
    
    
                objImage = New  _
                   MemoryStream(objwebClient.DownloadData(sURL))
                pb.Image = Image.FromStream(objImage)
                bAns = True
            Catch ex As Exception
    
                bAns = False
            End Try
    
            Return bAns
    
        End Function

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: help - PictureBox from Web(URL) [Smoothly]

    Always read the documentation first. You're using a PictureBox control. It already has inbuilt functionality to asynchronously load an image from the internet. Read the MSDN documentation for the LoadAsync method.

  3. #3

    Thread Starter
    Member intellilogic's Avatar
    Join Date
    Oct 2010
    Location
    Tyson's Corner, VA
    Posts
    39

    Re: help - PictureBox from Web(URL) [Smoothly]

    Perfect, thanks

    PictureBox.WaitOnLoad = False
    PictureBox.LoadAsync("http://www.sample.com/image.jpg")

Tags for this Thread

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