469,364 Members | 2,346 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

download an image and save locally

Hi,

I want to download an image from the web and save it locally. I have
spent all day messing about with this and am still no where. We have
a firewall so i use the below code. I know it works down to the line
as i also use this code to download an xml file from the net. I have
been messing around with all sorts of ways to stream the image into a
local file and save it. but no luck.

Am i totally off key here, can anyone point me in the right direction
to do what i thought would be a simple thing.
<code>

Dim sURL As String
sURL = "http://www.google.ie/images/hp0.gif"

Dim wrGETURL As WebRequest
wrGETURL = WebRequest.Create(sURL)
Dim saByPassList() As String
Dim myProxy As New WebProxy("10.32.0.20:8080", True,
saByPassList, New NetworkCredential("user", "password", "domain"))

myProxy.BypassProxyOnLocal = True
wrGETURL.Proxy = myProxy
Dim objStream As Stream
objStream = wrGETURL.GetResponse.GetResponseStream()

---------------------------------------

Dim br As New BinaryReader(objStream)

Dim arrPicture() As Byte = br.ReadBytes(objStream.Length)
</code>

Thanks.
Nov 18 '05 #1
8 2168
When you view an image in a web page, it is already downloaded. The user can
right-click the image to save it to another location.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"kieran" <ki********@hotmail.com> wrote in message
news:b3**************************@posting.google.c om...
Hi,

I want to download an image from the web and save it locally. I have
spent all day messing about with this and am still no where. We have
a firewall so i use the below code. I know it works down to the line
as i also use this code to download an xml file from the net. I have
been messing around with all sorts of ways to stream the image into a
local file and save it. but no luck.

Am i totally off key here, can anyone point me in the right direction
to do what i thought would be a simple thing.
<code>

Dim sURL As String
sURL = "http://www.google.ie/images/hp0.gif"

Dim wrGETURL As WebRequest
wrGETURL = WebRequest.Create(sURL)
Dim saByPassList() As String
Dim myProxy As New WebProxy("10.32.0.20:8080", True,
saByPassList, New NetworkCredential("user", "password", "domain"))

myProxy.BypassProxyOnLocal = True
wrGETURL.Proxy = myProxy
Dim objStream As Stream
objStream = wrGETURL.GetResponse.GetResponseStream()

---------------------------------------

Dim br As New BinaryReader(objStream)

Dim arrPicture() As Byte = br.ReadBytes(objStream.Length)
</code>

Thanks.

Nov 18 '05 #2
By default images are displayed in browsers.
If you want it to prompt a save option you might try putting it in a zip
file.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"kieran" <ki********@hotmail.com> wrote in message
news:b3**************************@posting.google.c om...
Hi,

I want to download an image from the web and save it locally. I have
spent all day messing about with this and am still no where. We have
a firewall so i use the below code. I know it works down to the line
as i also use this code to download an xml file from the net. I have
been messing around with all sorts of ways to stream the image into a
local file and save it. but no luck.

Am i totally off key here, can anyone point me in the right direction
to do what i thought would be a simple thing.
<code>

Dim sURL As String
sURL = "http://www.google.ie/images/hp0.gif"

Dim wrGETURL As WebRequest
wrGETURL = WebRequest.Create(sURL)
Dim saByPassList() As String
Dim myProxy As New WebProxy("10.32.0.20:8080", True,
saByPassList, New NetworkCredential("user", "password", "domain"))

myProxy.BypassProxyOnLocal = True
wrGETURL.Proxy = myProxy
Dim objStream As Stream
objStream = wrGETURL.GetResponse.GetResponseStream()

---------------------------------------

Dim br As New BinaryReader(objStream)

Dim arrPicture() As Byte = br.ReadBytes(objStream.Length)
</code>

Thanks.

Nov 18 '05 #3
Hi,
I need to save a certain image each day and display it locally as some
users do not have internet access. the image changes every day.

Any ideas...

"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message news:<#0**************@TK2MSFTNGP10.phx.gbl>...
By default images are displayed in browsers.
If you want it to prompt a save option you might try putting it in a zip
file.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"kieran" <ki********@hotmail.com> wrote in message
news:b3**************************@posting.google.c om...
Hi,

I want to download an image from the web and save it locally. I have
spent all day messing about with this and am still no where. We have
a firewall so i use the below code. I know it works down to the line
as i also use this code to download an xml file from the net. I have
been messing around with all sorts of ways to stream the image into a
local file and save it. but no luck.

Am i totally off key here, can anyone point me in the right direction
to do what i thought would be a simple thing.
<code>

Dim sURL As String
sURL = "http://www.google.ie/images/hp0.gif"

Dim wrGETURL As WebRequest
wrGETURL = WebRequest.Create(sURL)
Dim saByPassList() As String
Dim myProxy As New WebProxy("10.32.0.20:8080", True,
saByPassList, New NetworkCredential("user", "password", "domain"))

myProxy.BypassProxyOnLocal = True
wrGETURL.Proxy = myProxy
Dim objStream As Stream
objStream = wrGETURL.GetResponse.GetResponseStream()

---------------------------------------

Dim br As New BinaryReader(objStream)

Dim arrPicture() As Byte = br.ReadBytes(objStream.Length)
</code>

Thanks.

Nov 18 '05 #4
Dim fs As FileStream = File.Create("yourimage.jpg")
fs.Write(arrPicture, 0, arrPicture.Length)
fs.Close()

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"kieran" <ki********@hotmail.com> wrote in message
news:b3**************************@posting.google.c om...
Hi,
I need to save a certain image each day and display it locally as some
users do not have internet access. the image changes every day.

Any ideas...

"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message

news:<#0**************@TK2MSFTNGP10.phx.gbl>...
By default images are displayed in browsers.
If you want it to prompt a save option you might try putting it in a zip
file.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"kieran" <ki********@hotmail.com> wrote in message
news:b3**************************@posting.google.c om...
Hi,

I want to download an image from the web and save it locally. I have
spent all day messing about with this and am still no where. We have
a firewall so i use the below code. I know it works down to the line
as i also use this code to download an xml file from the net. I have
been messing around with all sorts of ways to stream the image into a
local file and save it. but no luck.

Am i totally off key here, can anyone point me in the right direction
to do what i thought would be a simple thing.
<code>

Dim sURL As String
sURL = "http://www.google.ie/images/hp0.gif"

Dim wrGETURL As WebRequest
wrGETURL = WebRequest.Create(sURL)
Dim saByPassList() As String
Dim myProxy As New WebProxy("10.32.0.20:8080", True,
saByPassList, New NetworkCredential("user", "password", "domain"))

myProxy.BypassProxyOnLocal = True
wrGETURL.Proxy = myProxy
Dim objStream As Stream
objStream = wrGETURL.GetResponse.GetResponseStream()

---------------------------------------

Dim br As New BinaryReader(objStream)

Dim arrPicture() As Byte = br.ReadBytes(objStream.Length)
</code>

Thanks.

Nov 18 '05 #5
Hi kieran:

It's possible the stream cannot be read all at once.

See:
Reading binary data in C#
http://www.yoda.arachsys.com/csharp/readbinary.html

--
Scott
http://www.OdeToCode.com

On 5 Aug 2004 01:13:40 -0700, ki********@hotmail.com (kieran) wrote:
Hi,
I need to save a certain image each day and display it locally as some
users do not have internet access. the image changes every day.

Any ideas...


Nov 18 '05 #6
Hi,

I now have the below code that will download an image from the web
through my firewall and save it locally. Great!

However the image I want to download updates daily and conseqently its
title changes daily.

However only a certain part of the title changes. i.e. the image is
desktop56247.gif. The image the next day will be desktop4112.gif.
There is no other image on the page whose title starts with 'desktop'.
Therefore, I need some way of saving an image at a particular url that
has 'desktop' and '.gif' in its title.
Any ideas and help much appreciated on how to figure this out.

<code>
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim MyImage As System.Drawing.Image
Dim URL As String = "http://www.google.com/images/logo.gif"
Dim FileName As String, URLpieces As String()
URLpieces = Split(URL, "/")
FileName = URLpieces.GetValue(UBound(URLpieces))

MyImage = GetImage(URL)
MyImage.Save("D:\temp\image.gif")
MyImage = Nothing
Close()
End Sub

Function GetImage(ByVal URL As String) As System.Drawing.Image
Dim Request As System.Net.HttpWebRequest
Dim Response As System.Net.HttpWebResponse

Try
Request = System.Net.WebRequest.Create(URL)
Dim saByPassList() As String

Dim myProxy As New System.Net.WebProxy("10.32.0.20:8080",
True, saByPassList, New System.Net.NetworkCredential("user",
"password", "domain"))

myProxy.BypassProxyOnLocal = True

Request.Proxy = myProxy
Response = CType(Request.GetResponse,
System.Net.WebResponse)
If Request.HaveResponse Then
If Response.StatusCode = Net.HttpStatusCode.OK Then

GetImage =
System.Drawing.Image.FromStream(Response.GetRespon seStream)

End If

End If
Catch e As System.Net.WebException

MsgBox("A web exception has occured [" & URL & "]." &
vbCrLf & " System returned: " & e.Message, MsgBoxStyle.Exclamation,
"Error!")
Exit Try

Catch e As System.Net.ProtocolViolationException

MsgBox("A protocol violation has occured [" & URL & "]." &
vbCrLf & " System returned: " & e.Message, MsgBoxStyle.Exclamation,
"Error!")
Exit Try
Catch e As System.Net.Sockets.SocketException
MsgBox("Socket error [" & URL & "]." & vbCrLf & " System
returned: " & e.Message, MsgBoxStyle.Exclamation, "Error!")
Exit Try
Catch e As System.IO.EndOfStreamException
MsgBox("An IO stream exception has occured. System
returned: " & e.Message, MsgBoxStyle.Exclamation, "Error!")

Exit Try
Finally
End Try
End Function

</code>
Nov 18 '05 #7
Okay, a web server is similar to a file server in that it can fetch files
for you, which is what you're trying to do. It is also like a file server in
that, in order to request a file, you have to know the file name. A file
server can return a listing of the contents of a directory, which is useful
for selecting files to request. A web server also has that capability,
although you can configure the web server to not allow directory browsing.
So, the solution to your problem lies in first determining whether or not
directory browsing is allowed.

If Directory Browsing IS allowed:
Request the directory, and parse the response to get the file names of
all files in the folder. Identify the one with the naming convention you've
described, and download it by requesting it by name (URL)

If Directory Browsing IS NOT allowed:
Set up a loop to attempt to download all combinations of possible file
names. Eventually, you'll hit the right one.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"kieran" <ki********@hotmail.com> wrote in message
news:b3**************************@posting.google.c om...
Hi,

I now have the below code that will download an image from the web
through my firewall and save it locally. Great!

However the image I want to download updates daily and conseqently its
title changes daily.

However only a certain part of the title changes. i.e. the image is
desktop56247.gif. The image the next day will be desktop4112.gif.
There is no other image on the page whose title starts with 'desktop'.
Therefore, I need some way of saving an image at a particular url that
has 'desktop' and '.gif' in its title.
Any ideas and help much appreciated on how to figure this out.

<code>
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim MyImage As System.Drawing.Image
Dim URL As String = "http://www.google.com/images/logo.gif"
Dim FileName As String, URLpieces As String()
URLpieces = Split(URL, "/")
FileName = URLpieces.GetValue(UBound(URLpieces))

MyImage = GetImage(URL)
MyImage.Save("D:\temp\image.gif")
MyImage = Nothing
Close()
End Sub

Function GetImage(ByVal URL As String) As System.Drawing.Image
Dim Request As System.Net.HttpWebRequest
Dim Response As System.Net.HttpWebResponse

Try
Request = System.Net.WebRequest.Create(URL)
Dim saByPassList() As String

Dim myProxy As New System.Net.WebProxy("10.32.0.20:8080",
True, saByPassList, New System.Net.NetworkCredential("user",
"password", "domain"))

myProxy.BypassProxyOnLocal = True

Request.Proxy = myProxy
Response = CType(Request.GetResponse,
System.Net.WebResponse)
If Request.HaveResponse Then
If Response.StatusCode = Net.HttpStatusCode.OK Then

GetImage =
System.Drawing.Image.FromStream(Response.GetRespon seStream)

End If

End If
Catch e As System.Net.WebException

MsgBox("A web exception has occured [" & URL & "]." &
vbCrLf & " System returned: " & e.Message, MsgBoxStyle.Exclamation,
"Error!")
Exit Try

Catch e As System.Net.ProtocolViolationException

MsgBox("A protocol violation has occured [" & URL & "]." &
vbCrLf & " System returned: " & e.Message, MsgBoxStyle.Exclamation,
"Error!")
Exit Try
Catch e As System.Net.Sockets.SocketException
MsgBox("Socket error [" & URL & "]." & vbCrLf & " System
returned: " & e.Message, MsgBoxStyle.Exclamation, "Error!")
Exit Try
Catch e As System.IO.EndOfStreamException
MsgBox("An IO stream exception has occured. System
returned: " & e.Message, MsgBoxStyle.Exclamation, "Error!")

Exit Try
Finally
End Try
End Function

</code>

Nov 18 '05 #8
Great.

Thanks for all the help.

I'm going to study into this and hopefully apply it for my problem.

Thanks.
Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by JS | last post: by
8 posts views Thread by Chris Dewin | last post: by
16 posts views Thread by Bob Moore | last post: by
reply views Thread by kieran | last post: by
2 posts views Thread by standish22 | last post: by
12 posts views Thread by Bob Bedford | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.