2
// Create the image element.
Image simpleImage = new Image();    
simpleImage.Width = 100;
simpleImage.Margin = new Thickness(5);

// Create source.
BitmapImage bi = new BitmapImage();

How should I set the Source for simpleImage to bi?

2 Answers 2

2
// Create the image element.
Image simpleImage = new Image();    
simpleImage.Width = 200;
simpleImage.Margin = new Thickness(5);

// Create source.
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit();
bi.UriSource = new Uri(@"/Images/1.jpg",UriKind.RelativeOrAbsolute);

bi.EndInit();
// Set the image source.
simpleImage.Source = bi;
Sign up to request clarification or add additional context in comments.

1 Comment

"BitmapImage.UriSource must be in a BeginInit/EndInit block" is not necessary. BitmapImage has a constructor that accepts an Uri parameter. Hence you can simply write simpleImage.Source = new BitmapImage(new Uri(...));.
2
var uri = new Uri(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg");
var bi = new BitmapImage(uri);
simpleImage.Source = bi;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.