Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: UWP C# Retreieve and display an image from URL?

Morgan Proffitt:

I have a small application which uses a small text document and searches through the lines for items. I am trying to get the program to grab an image from a page which would correspond to the search. I cannot get anything working to download the image. I have tried multiple solutions involving async and httpclient. But I really can't apply anything to work. I currently have this which MAY work, but I am a total noob to all this stuff, I enjoy creating little UI apps, I've never worked outside of the methods given.


public async static Task LoadImage(Uri uri)
{
BitmapImage bitmapImage = new BitmapImage();
string url = "http://ift.tt/2DU1iFi";

try
{
var rootFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync(@"Assets/images/", CreationCollisionOption.OpenIfExists);

var coverpic_file = await rootFolder.CreateFileAsync("exotic", CreationCollisionOption.FailIfExists);
try
{
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); // Create HttpClient
byte[] buffer = await client.GetByteArrayAsync(url); // Download file
using (Stream stream = await coverpic_file.OpenStreamForWriteAsync())
stream.Write(buffer, 0, buffer.Length); // Save
}
catch
{
}
}
catch
{
}

return null;
}

How do I get this to run on the click of a button? Or even, a better solution. Cheers



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: UWP C# Retreieve and display an image from URL?

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×