12

I have a web URL for the image. For example "http://testsite.com/web/abc.jpg". I want copy that URL in my local folder in "c:\images\"; and also when I copy that file into folder, I have to rename the image to "c:\images\xyz.jpg".

How can we do that?

Mohamed El-Nakeep
  • 6,580
  • 4
  • 35
  • 39
James123
  • 11,184
  • 66
  • 189
  • 343

4 Answers4

29

Request the image, and save it. For example:

byte[] data;
using (WebClient client = new WebClient()) {
  data = client.DownloadData("http://testsite.com/web/abc.jpg");
}
File.WriteAllBytes(@"c:\images\xyz.jpg", data);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    I like this answer as opposed to directly using DownloadFile, as it gives an opportunity to compare the data before committing it to a file. – MikeDub Nov 18 '16 at 21:18
10

You could use a WebClient:

using (WebClient wc = new WebClient())
    wc.DownloadFile("http://testsite.com/web/abc.jpg", @"c:\images\xyz.jpg");

This assumes you actually have write rights to the C:\images folder.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • 2
    Well try it with a URL that you are authorized to download from, otherwise you need to supply the WebClient with credentials that work on the target server. – BrokenGlass Apr 28 '11 at 21:09
3

thats not too difficult. Open a WebCLient and grab the bits, save them locally....

using ( WebClient webClient = new WebClient() ) 
{
   using (Stream stream = webClient.OpenRead(imgeUri))
   {
      using (Bitmap bitmap = new Bitmap(stream))
      {
         stream.Flush();
         stream.Close();
         bitmap.Save(saveto);
      }
   }
}
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
1
string path = "~/image/"; 
string picture = "Your picture name with extention";
path = Path.Combine(Server.MapPath(path), picture);
using (WebClient wc = new WebClient())
                            {
wc.DownloadFile("http://testsite.com/web/abc.jpg", path);
                            }

Its works for me

AbdusSalam
  • 420
  • 6
  • 10