Saving image to file from a webpage

G

Guest

I cant seem to find the commmand that I can use to save an image file
from a url to my local harddrive. I am looking to save a jpg from a
webpage that I have screen scraped.


I have a web application that uses readHtmlPage(url.ToString()); to
read the page in, I am able to use regular expressions to save to url
of the jpg to a string but I cant seem to find the way to save that
image to a file on my local drive

What I want to do is, if I have:

strJPGURL="http://www.website.com/images/­image123.jpg"


how can I save that to
f::/images/image123.jpg

Thanks..
Rich
 
O

Oddball

The usual disclaimer apples here - this was written by me so it's probably wrong. It
could do this task better - it could use async callbacks and all that gubbins. I don't
know how to use that (If anyone out there can post a better example with better
explinations that the one in the SDK I'll be VERY greatful - Async Callbacks have me
stumped).

But it works and it will stream an image from the interweb for you - just pass it the url.
From there just write it to a file.

-- BEGIN CODE --

private Image StreamImageFromNet(string url)
{
Image img = null;
Stream str = null;

System.Console.WriteLine("Retreiving: " + url);

HttpWebRequest webReq;
webReq =
(HttpWebRequest)HttpWebRequest.Create(url);

try
{
HttpWebResponse webResp =
(HttpWebResponse)webReq.GetResponse();
str = webResp.GetResponseStream();

img = Image.FromStream(str);

System.Console.WriteLine("That went off
without a hitch");
}
catch (WebException ex)
{
// Do something more important here
System.Console.WriteLine(ex.Message);
}
finally
{
if (str != null)
str.Close();
return img;
}

-- END CODE --

I cant seem to find the commmand that I can use to save an image file
from a url to my local harddrive. I am looking to save a jpg from a
webpage that I have screen scraped.


I have a web application that uses readHtmlPage(url.ToString()); to
read the page in, I am able to use regular expressions to save to url
of the jpg to a string but I cant seem to find the way to save that
image to a file on my local drive

What I want to do is, if I have:

strJPGURL="http://www.website.com/images/­image123.jpg"


how can I save that to
f::/images/image123.jpg

Thanks..
Rich


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

Another unchecked rambeling brought to you by:

Oddball
joshua@bf#N0SP4M#wd.co.uk
 
M

Morten Wennevik

Hi Rich,

In Addition to Oddballs example of using WebRequest, there is always the

WebClient.DownloadFile(url, localfilename)

It is less flexible than WebRequest/Response but far easier to use.
 
O

Oddball

Crikey - that is easy isn't it....

Nice one Morten!


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

Another unchecked rambeling brought to you by:

Oddball
joshua@bf#N0SP4M#wd.co.uk
 

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

Top