1

I writing application for Windows 10 mobile.

I have Image in my XAML.

<Image x:Name="productimage" HorizontalAlignment="Left" Height="146" VerticalAlignment="Top" Width="135"/>

I need to download image from URL and put it to Image

How I can do this?

Thank's with help.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • Possible duplicate of [Programmatically set the Source of an Image (XAML)](http://stackoverflow.com/questions/11267257/programmatically-set-the-source-of-an-image-xaml) – Eldar Dordzhiev Apr 13 '16 at 17:55
  • This is not duplicate, because those answers don't solve my problem. Anyone of this code don't work. And I have UWP app. @EldarDordzhiev –  Apr 13 '16 at 18:11
  • The question I referenced is the exact solution for your issue. UWP and Windows 8 XAML UI are basically the same UI frameworks, the solution applies to both. If the code doesn't work, then share your efforts with us. Спасибо. – Eldar Dordzhiev Apr 13 '16 at 19:44

3 Answers3

1

You can try this snipet

System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

...

System.Net.Http.HttpResponseMessage imageResponse = await client.GetAsync(imageUrl);

// A memory stream where write the image data
Windows.Storage.Streams.InMemoryRandomAccessStream randomAccess =
new Windows.Storage.Streams.InMemoryRandomAccessStream();

Windows.Storage.Streams.DataWriter writer = 
new Windows.Storage.Streams.DataWriter(randomAccess.GetOutputStreamAt(0));

// Write and save the data into the stream
writer.WriteBytes(await imageResponse.Content.ReadAsByteArrayAsync());
await writer.StoreAsync();

// Create a Bitmap and assign it to the target Image control
Windows.UI.Xaml.Media.Imaging.BitmapImage bm =
new Windows.UI.Xaml.Media.Imaging.BitmapImage();
await bm.SetSourceAsync(randomAccess);
productimage.Source = bm;

It's not my snipet, but should work also with UWP. UWP is very similar to Windows 8.1. So you can try to search for WinRT examples

Alexej Sommer
  • 2,677
  • 1
  • 14
  • 25
1

I found answer on my question

It's simple

string url = "url";
productimage.Source = new BitmapImage(new Uri(url, UriKind.Absolute));

Thank's to @ Eldar Dordzhiev

0

This code is in VB.Net. I guess you could read it and convert to C#. I just wanted to help. Use Windows.Web.Http for UWP as System.Net.Htpp is being depreciated.

Imports Windows.Web.Http
...
Dim client as New HttpClient
Dim response = Await client.GetBufferAsync(ImgUri)
stream = response.AsStream
Dim mem = New MemoryStream()
Await stream.CopyToAsync(mem)
mem.Position = 0
Dim img As New BitmapImage
img.SetSource(mem.AsRandomAccessStream)
Return img
Raamakrishnan A.
  • 515
  • 3
  • 14