如何在 Windows Phone 8.1 中下载图像

How to download an image in Windows Phone 8.1

我正在制作一个显示来自 url 的图像的应用程序,将适当的 URI 设置为 Image.Source

我想添加下载图片功能,以允许用户将图片下载到他的设备

如何将图像下载到本地存储(我应该使用什么文件夹?)

谢谢

您可以使用此代码:

string localFilename = @"c:\localpath\tofile.jpg";
using(WebClient client = new WebClient())
{
    client.DownloadFile("http://www.example.com/image.jpg", localFilename);
}

更多信息:Download image from the site in .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。将您的图像 Url 作为参数传递给脚本。 (此处未显示)

Invoke-WebRequest -Uri ('http://XXX/image.php?roll=123) -OutFile ('img.jpeg') -Verbose