Question

I am trying to add Images to a GridView, but the images never actually show. Instead, I'm just display the class name in a Thumbnail looking layout.

Here is the code that I have been tweaking

public async void setImage(string url) {

    Byte[] imageBytes = await httpClient.GetByteArrayAsync(url);

    MemoryStream stream = new MemoryStream(imageBytes);
    var randomAccessStream = new MemoryRandomAccessStream(stream);

    BitmapImage image = new BitmapImage();
    await image.SetSourceAsync(randomAccessStream);

    imagesGrid.Items.Add(image);           
}

Thanks for the help. I ended up with the following block that works.

public void setImage(string url) {
    Image img = new Image();
    img.Source = new BitmapImage(new Uri(url));

    imagesGrid.Items.Add(img);           
}
Was it helpful?

Solution

This is because you're adding BitmapImages to your visual tree. BitmapImages are not visual elements. What you need to do is create an Image and set the Source property to the URI of your image file. There are a number of examples of this in xaml and also code on my blog here.

I haven't tested this but it should be something like this:

Image img = new Image();
img.Source = new BitmapImage(new Uri(url));
imagesGrid.Items.Add(img);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top