簡體   English   中英

如何在Windows Phone 8.1中下載圖像

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM