繁体   English   中英

下载图像UWP(C#)

[英]Download Image UWP (C#)

我为Windows 10移动版编写应用程序。

我的XAML中有图片。

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

我需要从URL下载图像并将其放入图像

我该怎么做?

谢谢帮忙。

我找到了问题的答案

这很简单

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

感谢@ Eldar Dordzhiev

你可以试试这个片段

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;

这不是我的提神,但也应该与UWP一起使用。 UWP与Windows 8.1非常相似。 因此,您可以尝试搜索WinRT示例

这段代码在VB.Net中。 我想您可以阅读并转换为C#。 我只是想帮忙。 使用UWP的Windows.Web.Http ,因为System.Net.Htpp已被贬值。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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