Download image and Save to file

  • Thread starter Thread starter TEAMBEER
  • Start date Start date
T

TEAMBEER

Hi all,

I am trying to download an image from an url and save this image to a file.
The following procedure saves a file with 0 bytes. why??


Public Sub LoadImageFromUrl(ByVal Url As String)

Dim srv_request As System.Net.WebRequest =
System.Net.WebRequest.Create(Url)
Dim srv_response As System.Net.WebResponse = srv_request.GetResponse()
Dim ImgStream As IO.Stream
Dim buf(8192) As Byte
Dim read As Integer

ImgStream = srv_response.GetResponseStream()

Dim Image As System.Drawing.Image = New System.Drawing.Bitmap(ImgStream)

Dim fs As IO.FileStream

fs = New IO.FileStream("\Temp\test.jpg", IO.FileMode.Create,
IO.FileAccess.Write)

Do While read <> 0
read = ImgStream.Read(buf, 0, buf.Length)
If read <= 0 Then
Exit Do
End If
fs.Write(buf, 0, read)
Loop

fs.Close()

End Function





THANKS,
Herwig
 
At first glance it looks okay.

Have you tried verifying the stream information before reading it? Before
your While loop you may want to set the current position to 0 and verify the
size. Maybe the Bitmap constructor does something to it? Have you tried
commenting out the Image setting code?

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Actually, I found the problem. The variable "read" is set to 0 so you are
never getting into the while loop to begin with. Try adding the following
line before the while loop starts...

read = ImgStream.Read(buf, 0, buf.Length)
Do While read <> 0
read = ImgStream.Read(buf, 0, buf.Length)
If read <= 0 Then
Exit Do

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top