Download an image to local storage in Metro style apps

前端 未结 5 1221
一向
一向 2020-12-16 07:22

In WinRT / C#, How do I download an image to a local folder to support caching of an online catalogue for offline use? is there a way to directly download the images and lin

相关标签:
5条回答
  • 2020-12-16 07:29

    Looks like the code below from the HttpClient sample for Windows 8 solves the issue

        HttpRequestMessage request = new HttpRequestMessage(
            HttpMethod.Get, resourceAddress);
        HttpResponseMessage response = await rootPage.httpClient.SendAsync(request,
            HttpCompletionOption.ResponseHeadersRead);
    

    httpClient is a HttpClient, and its BaseAddress needs to be set a the server folder of your resource. we can then do this to convert that to an image source (if that's what we're downloading)

        InMemoryRandomAccessStream randomAccessStream = 
            new InMemoryRandomAccessStream();
        DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
        writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());
        await writer.StoreAsync();
        BitmapImage image = new BitmapImage();
        imagecontrol.SetSource(randomAccessStream);
    

    or this to write it to file

        var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
             filename, CreationCollisionOption.ReplaceExisting);
        var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite);
        DataWriter writer = new DataWriter(fs.GetOutputStreamAt(0));
        writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());
        await writer.StoreAsync();
        writer.DetachStream();
        await fs.FlushAsync();
    
    0 讨论(0)
  • 2020-12-16 07:33

    Try this:

            var response = await HttpWebRequest.Create(url).GetResponseAsync();
            List<Byte> allBytes = new List<byte>();
            using (Stream imageStream = response.GetResponseStream())
            {
                byte[] buffer = new byte[4000];
                int bytesRead = 0;
                while ((bytesRead = await imageStream.ReadAsync(buffer, 0, 4000)) > 0)
                {
                    allBytes.AddRange(buffer.Take(bytesRead));
                }
            }
            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
                        "localfile.png", CreationCollisionOption.FailIfExists);
            await FileIO.WriteBytesAsync(file, allBytes.ToArray()); 
    
    0 讨论(0)
  • 2020-12-16 07:33

    GetResponseAsync will give you a WebResponse file not the image:

    Stream imageStream = downloadedimage.GetResponseStream();
    

    Then you can write to the image file:

    byte[] imageData = new byte[imageStream.Length];
    using(StreamReader reader = new Streamreader(imageStream))
    {
        reader.ReadBytes(imageData, 0, imageData.Length);
    }
    await FileIO.WriteBytesAsync(imgfile, imageData);
    
    0 讨论(0)
  • 2020-12-16 07:42

    By default Bitmap Images are cached by WinRT with no intervention required by the developer.

    0 讨论(0)
  • 2020-12-16 07:45

    If you want to download a file represented by a URL, the easiest way is to use the BackgroundDownloader class. See this page for more information on how to use the background downloader.

    0 讨论(0)
提交回复
热议问题