Results 1 to 20 of 20

Thread: Weird problem using WebClient to download...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Weird problem using WebClient to download...

    Hi i am using WebClient do download a picture/image. The code I am using is:

    VB Code:
    1. Dim m1(-1) As Byte
    2.         Dim url As String = "urlhere"
    3.         Dim dbyte As New System.Net.WebClient
    4.        
    5.         dbyte.Headers.Add("Cookie", CookieString)
    6.  
    7.         Dim memstream As New System.IO.MemoryStream
    8.  
    9.         m1 = dbyte.DownloadData(url)
    10.         memstream.Write(m1, 0, m1.Length)
    11.         PictureBox1.Image = Image.FromStream(memstream)
    12.         PictureBox1.Size = PictureBox1.Image.Size
    13.         PictureBox1.Refresh()

    If I load up my app, and use this code, it will freeze for about 5-10 seconds and then download the image and display it fine. If I continue using the code it will work fine and download the image normally without freezing.

    Can anyone help me stop it from freezing the first time it is used? Thanks

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Weird problem using WebClient to download...

    I have the same issue. I could not find a solution to it so I gave it up kind of. Please if you find the solution to that post it here.

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

    Re: Weird problem using WebClient to download...

    I am not sure why you have the need to add cookies (as I don't know what is in your cookiestring variable).

    Take a look at the link in my signature for downloading an image from the web to a picturebox, it's code is a little simpler, and may work faster for you.

    For one it creates the memory stream's contents in its constructor instead of creating the stream and THEN writing to it.

    It also passes the memory stream to the bitmap constructor to make the image versus calling Image.FromStream..

    I am not sure which ones, if any of these things will speed up the process, all I can tell you is in my testing of my code, its a lot faster that 5-10 seconds on the first run.

    What could be the issue is an exception is being thrown inside one of the .NET classes, but the exception isn't a deal breaker, and the method still works due to internal error handling. Generally this will cause a delay on the first exception, but subsequent ones will not have the delay. A way to test if this is infact the issue, would be to compile your app in release mode, and run the exe. This delay does not happen on release compiled exes.

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Weird problem using WebClient to download...

    Hi Kleinman, I downloaded the WebDownloader project on your signature and tried it which takes the same time to download the file as my app (32 seconds). I don’t know what the problem is. I guess that is the way it spouse to be.

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

    Re: Weird problem using WebClient to download...

    I was talking about the "Display Internet Image in Picturebox", not the "Download Files From Web With Progress Bar"

    did you get the right one?

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Weird problem using WebClient to download...

    I am sorry for confusion, I tried the WebDownloader because I have the same issue but with downloading a file so I decided to try WebDownloader on your signature to compare the download time with my app which was the same.
    And yes I tried the “Display Internet Image in Picturebox" which took about 31 - 34 seconds to download the image at " http://image30.webshots.com/31/6/70/46/2068670460029721934MwPunV_fs.jpg" this Url.

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

    Re: Weird problem using WebClient to download...

    that image took about 30 seconds to download right in IE for me...

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: Weird problem using WebClient to download...

    The cookie string has nothing to do with the image downloading, it is just needed as part of my app. Anyway the problem is that after the app has been loaded, only the first time the code is used does it freeze up my app, if i use the code again and again after that the image downloads fine.

    thanks for posting your code klienma im just gonna try it in my app
    Last edited by 4xzer0; Aug 28th, 2006 at 02:41 PM.

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Weird problem using WebClient to download...

    It only takes long time to download at the first time. After that it takes about 3 seconds. So I think you are right that there may be so exception being throws or maybe there is some window service that has to be connected for the app to download the file.

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

    Re: Weird problem using WebClient to download...

    Again, compile the app in release mode, and try it again. It may be due to a handled internal exception, which causes delay when in debug mode in the IDE.

    If that is NOT the case, then it may be an issue of cache. Are you always downloading the same image? From the same site?

    The image may be cached if you are always downloading the same one, if you are downloading different images, then perhaps the initial webconnection that is made in the underlying code of the webclient keeps a connection until it times out (like database connections do) so subsequent connections are much faster that the initial one.

  11. #11
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Weird problem using WebClient to download...

    Well that picture is taking about 2 - 3 seconds for my Internet Explorer to show.

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

    Re: Weird problem using WebClient to download...

    Quote Originally Posted by VBDT
    Well that picture is taking about 2 - 3 seconds for my Internet Explorer to show.
    That is because you probably have it cached.

    Delete all your temporary internet files, and then see how long it takes

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: Weird problem using WebClient to download...

    Kleinma your code also seems to freeze up my app the first time it is used, but then works fine again every time after that. I did it in release and it still does the same thing. Oh well i guess i can live with it, it's just annoying :\
    Last edited by 4xzer0; Aug 28th, 2006 at 02:58 PM.

  14. #14
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Weird problem using WebClient to download...

    That is because you probably have it cached.

    Delete all your temporary internet files, and then see how long it takes
    Yes you are right!

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

    Re: Weird problem using WebClient to download...

    Then it must be part of the initial connection that the webclient needs to make. It probably retains some of the conneciton information for any subsequent calls to make a connection, so they happen quicker than the first one.

    you could download an image when the program loads so that the initial connection is made, and it won't feel as much of a delay

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: Weird problem using WebClient to download...

    yes that's a good idea

    Kleinma are you any good at working with pictures and their pixels etc. in vb?

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

    Re: Weird problem using WebClient to download...

    Quote Originally Posted by 4xzer0
    yes that's a good idea

    Kleinma are you any good at working with pictures and their pixels etc. in vb?
    I have done some work with images and pixels, but it depends on what you are looking to do. Can you be more specific at what you are working on?

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: Weird problem using WebClient to download...

    OK well it is an optical picture recogniser.

    I have posted all my current code here:
    http://rafb.net/paste/results/hsIk6n25.html

    The first for-next loop loops through the pixels in the image and makes all the blue and green ones equal to red Xor 255. The second for-next loop I haven't quite finished yet and that is what i need help on. At the moment it loops through all the pixels and finds a pixel with a red value higher than 10 (it would be better if it found the pixel with the highest red value in the image but im not sure how to do that).

    Now all i really need help on is returning the co-ordinates on the image of this pixel it has found with a red value of more than 10.
    This is the last bit of my app I have been working on for the last couple of weeks so if you could help me do this last bit it would be great

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

    Re: Weird problem using WebClient to download...

    for your second loop, (if I understand correctly) it sounds easy enough, just loop, but instead of looking for a value above 10, compare the value to the value from the previous iteration of the loop, and if its higher, replace that value and continue on.

    That probably sounds confusing so something like this (written in pseudocode)
    Code:
    HighestRed = 0 'integer
    loop pixels
       if redvalueofpixel > highestred then
            highestred = redvalueofpixel
       end if
    next
    that kind of loop will give highestred the highest value when its done.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: Weird problem using WebClient to download...

    oh yeh thanks

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