简体   繁体   中英

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

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#

using RestSharp library provides a structured way of handling images

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 . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM