Bug
Bug

Reputation: 185

Converting URI -> object -> imagesource

img = new Image()
{
    Height = 150,
    Stretch = System.Windows.Media.Stretch.Fill,
    Width = 200
};
img.Source = (ImageSource) new ImageSourceConverter()
                .ConvertFromString("/FirstDemo;component/Images/Hero.jpg");

After hours of research, trying to assign an image to an image class. I came across this way of assigning an image. I have absolutely no idea why I this code does not run. It does not get any compiler error though.. Odd. its 11 25 pm here btw

Upvotes: 2

Views: 10718

Answers (2)

brunnerh
brunnerh

Reputation: 185290

Your URI string is probably broken, see the reference for more detail on how it should be composed (you might be missing "pack://application:,,," at the beginning).

In any case you should usually not use the ImageSourceConverter in code, it is intended for the XAML parser.

Instead use BitmapImage:

img.Source = new BitmapImage(new Uri("..."));

Upvotes: 2

MyKuLLSKI
MyKuLLSKI

Reputation: 5325

Do it this way:

img = new Image();
img.Height = 150;
img.Width = 200;
img.Stretch = Stretch.Fill;
img.Source = new BitmapImage(new Uri("/FirstDemo;component/Images/Hero.jpg"));

Upvotes: 10

Related Questions