|
-
Aug 28th, 2006, 01:17 PM
#1
Thread Starter
Addicted Member
Weird problem using WebClient to download...
Hi i am using WebClient do download a picture/image. The code I am using is:
VB Code:
Dim m1(-1) As Byte
Dim url As String = "urlhere"
Dim dbyte As New System.Net.WebClient
dbyte.Headers.Add("Cookie", CookieString)
Dim memstream As New System.IO.MemoryStream
m1 = dbyte.DownloadData(url)
memstream.Write(m1, 0, m1.Length)
PictureBox1.Image = Image.FromStream(memstream)
PictureBox1.Size = PictureBox1.Image.Size
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
-
Aug 28th, 2006, 01:27 PM
#2
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.
-
Aug 28th, 2006, 01:43 PM
#3
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.
-
Aug 28th, 2006, 02:05 PM
#4
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.
-
Aug 28th, 2006, 02:06 PM
#5
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?
-
Aug 28th, 2006, 02:26 PM
#6
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.
-
Aug 28th, 2006, 02:29 PM
#7
Re: Weird problem using WebClient to download...
that image took about 30 seconds to download right in IE for me...
-
Aug 28th, 2006, 02:36 PM
#8
Thread Starter
Addicted Member
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.
-
Aug 28th, 2006, 02:38 PM
#9
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.
-
Aug 28th, 2006, 02:41 PM
#10
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.
-
Aug 28th, 2006, 02:43 PM
#11
Re: Weird problem using WebClient to download...
Well that picture is taking about 2 - 3 seconds for my Internet Explorer to show.
-
Aug 28th, 2006, 02:44 PM
#12
Re: Weird problem using WebClient to download...
 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
-
Aug 28th, 2006, 02:53 PM
#13
Thread Starter
Addicted Member
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.
-
Aug 28th, 2006, 02:54 PM
#14
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!
-
Aug 28th, 2006, 02:55 PM
#15
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
-
Aug 28th, 2006, 02:57 PM
#16
Thread Starter
Addicted Member
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?
-
Aug 28th, 2006, 02:58 PM
#17
Re: Weird problem using WebClient to download...
 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?
-
Aug 28th, 2006, 03:08 PM
#18
Thread Starter
Addicted Member
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
-
Aug 28th, 2006, 03:44 PM
#19
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.
-
Aug 28th, 2006, 04:10 PM
#20
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|