[英]How to download an image in Windows Phone 8.1
I'm making an app that shows images from url setting the propper Uri to the Image.Source 我正在制作一个应用程序,该应用程序显示将url设置为proper Uri的图像从Image.Source
I want to add the download image functionallity in order to allow the user to download the image to his device 我要添加下载图像功能,以允许用户将图像下载到他的设备
How can I download the image to local storage (and what is the folder I should use for it?) 如何将映像下载到本地存储(以及应使用的文件夹是什么?)
Thanks 谢谢
You could use this code: 您可以使用以下代码:
string localFilename = @"c:\localpath\tofile.jpg";
using(WebClient client = new WebClient())
{
client.DownloadFile("http://www.example.com/image.jpg", localFilename);
}
More info: Download image from the site in .NET/C# 更多信息: 从.NET / C#网站下载图像
using RestSharp library provides a structured way of handling images 使用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; }));
;
}
});
}
The other easy way is to give the task to powershell
. 另一种简单的方法是将任务交给
powershell
。 Pass your image Url to the script as arguments. 将您的图片网址作为参数传递给脚本。 (Not shown here)
(此处未显示)
Invoke-WebRequest -Uri ('http://XXX/image.php?roll=123) -OutFile ('img.jpeg') -Verbose
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.