1

I am currently stuck on a piece of my project in Visual Studio 2017. The following code is meant to set the Source of an Image component on a .xaml page.

private void setImage()
    {
        var image = new Image();
        var imageUrl = @"http://ddragon.leagueoflegends.com/cdn/5.2.1/img/" +
        currentChamp.Image.Group + "/" + currentChamp.Image.Full;
        Debug.Write(imageUrl);
        BitmapImage bmi = new BitmapImage();
        bmi.BeginInit();
        bmi.UriSource = new Uri(imageUrl, UriKind.Absolute);
        bmi.EndInit();

        image.Source = bmi;
    }

Here is the code to be referenced. ^^^

In all other questions I have found similar to what I am asking, this has unanimously been the correct/answered/approved way of doing what I am trying to do. However, I am receiving an error that no one else has received in any of the question

cannot implicitly convert system.windows.media.imaging.BitmapImage to Windows.UI.XAML.media.ImageSource

My imports, references, and types have all been checked and are correct. Am I missing a framework? Was there an update that may have caused this to no longer work?

I am using VS2017 working in Microsoft.NETCore.UniversalWindowsPlatform

Note that this is not a duplicate of this, as I am not referencing a resource folder and my path is Absolute.

spaff
  • 163
  • 1
  • 16

2 Answers2

3

Windows.Media.Imaging.BitmapImage is a WPF class. Windows.UI.XAML.media.ImageSource is UWP. You found solutions for WPF, but your UWP Image class wants a UWP bitmap object. You need something using Windows.UI.Xaml.Media.Imaging.BitmapImage.

I'm unable to test UWP at the moment, but from reading the documentation, I think you want something like this:

image.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri(imageUrl, UriKind.Absolute));

It appears that you have using Windows.Media.Imaging; in your C# file. It would be wise to replace that with using Windows.UI.Xaml.Media.Imaging;

Then you could just use this:

image.Source = new BitmapImage(new Uri(imageUrl, UriKind.Absolute));
  • This worked perfectly, as did your example. Thank you very much, I would've never caught that. I am relatively new to c# and debugging has been my biggest challenge. – spaff Jun 30 '17 at 12:32
2

The error message here tells you exactly what is wrong. You are using a non-UWP BitmapImage in a UWP project. Get rid of your using for System.Windows.Media.Imaging and change it to Windows.UI.Xaml.Media.Imaging.

hoodaticus
  • 3,772
  • 1
  • 18
  • 28