How to copying image file from remote website to my server?

R

Ray Price

I want to use a server-side C# script to copy an image from a remote URL to
a local file in the web directory. I tried using File.Copy from the
System.IO namespace but it doesn't support URL.

Can anyone shed some light on how to do this?

Thanks

Ray
 
S

Steven Cheng[MSFT]

Hi Ray,

As for the retrieving images from remote site via web url address problem,
I think we can choose either the
"WebClient" or "WebRequest" class under the System.Net namespace.

1.The webclient class is a more powerful and encapsulated one, we can use
it to download web resource as below:

string url =
"http://msdn.microsoft.com/library/toolbar/3.0/images/banners/msdn_masthead_
ltr.gif";

System.Net.WebClient wc = new System.Net.WebClient();
wc.DownloadFile(url, "download.gif");

2.The WebRequest class is a bit a raw , since it is often used to retrieve
web content as stream(such as search engine or web spider). We can use the
WebRequest together with the Image class to download a remote image. For
example:

WebRequest wr = WebRequest.Create(url);
Image img = Image.FromStream(wr.GetResponse().GetResponseStream());
img.Save("mydownload.gif");

Hope these help. thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Ray,

Have you had a chance to check the suggestions in the former messages or
have you got the problem resolved? If there are anything else we can help,
please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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