download an image and save locally

K

kieran

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.
 
K

Kevin Spencer

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.
 
S

Steve C. Orr [MVP, MCSD]

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.
 
K

kieran

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...
 
K

Kevin Spencer

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 said:
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]" <[email protected]> wrote in message
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.
 
K

kieran

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.GetResponseStream)

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>
 
K

Kevin Spencer

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 said:
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.GetResponseStream)

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>
 
K

kieran

Great.

Thanks for all the help.

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

Thanks.
 
Joined
Sep 2, 2008
Messages
3
Reaction score
0
Save Image from HTTP URL - .NET, ASP.NET

just use this code:

mxdev.blogspot.com
/2008/12/get-image-by-url-net-aspnet.html

Solution:
1. get Stream from URL using WebRequest class
2. Create Image object from stream using System.Drawing.Image.FromStream method
3. Save file on disk using System.Drawing.Image.Save method



Code:

using System.Net;
using System.IO;

public bool getImageByUrl(string url, string filename)
{
imageType = "";

WebResponse response = null;
Stream remoteStream = null;
StreamReader readStream = null;
try
{
WebRequest request = WebRequest.Create(url);
if (request != null)
{
response = request.GetResponse();
if (response != null)
{
remoteStream = response.GetResponseStream();



string content_type = response.Headers["Content-type"];


string imageType = "";

if (content_type == "image/jpeg" || content_type == "image/jpg")
{
imageType = "jpg";
}
else if (content_type == "image/png")
{
imageType = "png";
}
else if (content_type == "image/gif")
{
imageType = "gif";
}
else
{
return false;
}


readStream = new StreamReader(remoteStream);

System.Drawing.Image img = System.Drawing.Image.FromStream(
remoteStream);

if (img == null)
return false;

img.Save( filename, System.Drawing.Imaging.ImageFormat.Jpeg );
img.Dispose();



}
}
}
finally
{
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (readStream != null) readStream.Close();
}

return true;

}

hope it helps

kieran said:
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.



Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top