[英]How to download an image in Windows Phone 8.1
我正在制作一个应用程序,该应用程序显示将url设置为proper Uri的图像从Image.Source
我要添加下载图像功能,以允许用户将图像下载到他的设备
如何将映像下载到本地存储(以及应使用的文件夹是什么?)
谢谢
您可以使用以下代码:
string localFilename = @"c:\localpath\tofile.jpg";
using(WebClient client = new WebClient())
{
client.DownloadFile("http://www.example.com/image.jpg", localFilename);
}
更多信息: 从.NET / C#网站下载图像
使用RestSharp库提供了一种处理图像的结构化方式
private void GetImage()
{
RestClient _Client = new RestClient(BASE_URI);
RestRequest request = new RestRequest("/api/img/{FileName}");
request.AddParameter("FileName", "dummy.jpg", ParameterType.UrlSegment);
_Client.ExecuteAsync(
request,
Response =>
{
if (Response != null)
{
byte[] imageBytes = Response.RawBytes;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(imageBytes);
bitmapImage.CreateOptions = BitmapCreateOptions.None;
bitmapImage.CacheOption = BitmapCacheOption.Default;
bitmapImage.EndInit();
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
Guid photoID = System.Guid.NewGuid();
String photolocation = String.Format(@"c:\temp\{0}.jpg", Guid.NewGuid().ToString());
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
using (var filestream = new FileStream(photolocation, FileMode.Create))
encoder.Save(filestream);
this.Dispatcher.Invoke((Action)(() => { img.Source = bitmapImage; }));
;
}
});
}
另一种简单的方法是将任务交给powershell
。 将您的图片网址作为参数传递给脚本。 (此处未显示)
Invoke-WebRequest -Uri ('http://XXX/image.php?roll=123) -OutFile ('img.jpeg') -Verbose
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.