0

I'm trying to download PNG images to file but I'm doing it through my company network proxy. I found this bit of code to download PNG images. Here's the code:

using (WebClient webClient = new WebClient()) 
{
    byte [] data = webClient.DownloadData("https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xpf1/v/t34.0-12/10555140_10201501435212873_1318258071_n.jpg?oh=97ebc03895b7acee9aebbde7d6b002bf&oe=53C9ABB0&__gda__=1405685729_110e04e71d9");

   using (MemoryStream mem = new MemoryStream(data)) 
   {
       using (var yourImage = Image.FromStream(mem)) 
       { 
          // If you want it as Png
           yourImage.Save("path_to_your_file.png", ImageFormat.Png) ; 
       }
   } 

}

I'm using this bit of code to get passed the proxy, but I'm not sure how to incorporate the code mentioned above to download the images:

HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.Proxy = new WebProxy(PROXY_URL, true);
clientHandler.Proxy.Credentials = new NetworkCredential(PROXY_UID, PROXY_PWD, PROXY_DMN);
var client = new HttpClient(clientHandler);
var result = client.GetStreamAsync(url).Result;

I think I'm close. What do I need to do to make this work?

inquisitive_one
  • 1,465
  • 7
  • 32
  • 56

3 Answers3

2

Try :

HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.Proxy = new WebProxy(PROXY_URL, true);
clientHandler.Proxy.Credentials = new NetworkCredential(PROXY_UID, PROXY_PWD, PROXY_DMN);
var client = new HttpClient(clientHandler);
var stream = client.GetStreamAsync(url).Result;
using (var yourImage = Image.FromStream(stream)) 
{ 
  // If you want it as Png
   yourImage.Save("path_to_your_file.png", ImageFormat.Png) ; 
}
Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53
1

The WebClient object in your using statement also has the Proxy property. You only need to include these lines directly inside your using statement:

webClient.Proxy = new WebProxy(PROXY_URL, true);
webClient.Proxy.Credentials = new NetworkCredential(PROXY_UID, PROXY_PWD, PROXY_DMN);

Note that I haven't tried this code. Just making an observation.

Matt Coats
  • 332
  • 1
  • 5
0

.net Framework allows PictureBox Control to Load Images from url

So Simply Load Image in Picture Box

and Save image in Laod Complete Event

pictureBox1.ImageLocation = "PROXY_URL;

void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
    {
   pictureBox1.Image.Save(destination);
}
Ali Humayun
  • 1,756
  • 1
  • 15
  • 12