question

SanchezRick-0238 avatar image
0 Votes"
SanchezRick-0238 asked NicoZhu-MSFT edited

Download image from http client and save in project UWP

I have a UWP application and I am not sure how to go about saving an image from the http response to a folder in the project. Thanks

var httpClient = new HttpClient();
HttpRequestMessage httpRequest;

             httpRequest = new HttpRequestMessage(HttpMethod.Get, "url" + id + "/photo/$value");
             httpRequest.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
             var response = await httpClient.SendAsync(httpRequest);

             if (response.IsSuccessStatusCode)
             {

                 //download images to photos and save
             }
         }
windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered NicoZhu-MSFT edited


Hello, Welcome to Micorosoft Q&A,

Download image from http client and save in project UWP

For your scenario, we suggest you use BitmapEncoder to write the httpclient response to the file like the following.


 using (HttpClient client = new HttpClient())
 {
     try
     {
         var response = await client.GetAsync(new Uri("url"));
    
         if (response != null && response.StatusCode == HttpStatusCode.Ok)
         {

              //download images to photos and save

             using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
             {
                 var buffer = await response.Content.ReadAsBufferAsync();
                 await stream.WriteAsync(buffer);
                 var savePicker = new FileSavePicker();
                 savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                 savePicker.FileTypeChoices.Add("Image", new List<string>() { ".jpg" });
                 savePicker.SuggestedFileName = "Card" + DateTime.Now.ToString("yyyyMMddhhmmss");
                 StorageFile savefile = await savePicker.PickSaveFileAsync();
                 if (savefile == null)
                     return;
    
                 BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
                 PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
    
                 using (IRandomAccessStream filestream = await savefile.OpenAsync(FileAccessMode.ReadWrite))
                 {
                     var encoder = await
                     BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, filestream);
    
                     encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                             BitmapAlphaMode.Ignore,
                                             (uint)decoder.PixelWidth,
                                         (uint)decoder.PixelHeight,
                                             200,
                                             200,
                                             pixelData.DetachPixelData());
    
                     await encoder.FlushAsync();
                 }
    
             }
    
         }
     }
     catch (Exception ex)
     {
    
     }
 }




If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.






5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.