我尝试将PNG图像下载到文件中,但我是通过公司网络代理进行的。我发现这个bit of code下载了PNG图像。这是代码:
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) ;
}
}
}
我使用这段代码来通过代理,但我不确定如何合并上面提到的代码来下载图片:
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;
我想我已经接近了。我需要做些什么来完成这项工作?
答案 0 :(得分:2)
尝试:
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) ;
}
答案 1 :(得分:1)
using语句中的WebClient对象也具有Proxy属性。您只需要在using语句中直接包含这些行:
webClient.Proxy = new WebProxy(PROXY_URL, true);
webClient.Proxy.Credentials = new NetworkCredential(PROXY_UID, PROXY_PWD, PROXY_DMN);
请注意,我还没有尝试过此代码。只是做一个观察。
答案 2 :(得分:0)
.net Framework允许PictureBox控件从URL加载图像
所以只需在图片框中加载图片
并在Laod Complete Event中保存图像
pictureBox1.ImageLocation = "PROXY_URL;
void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
pictureBox1.Image.Save(destination);
}