如何在Wpf中设置图像的源?

如何在Wpf中设置图像的源?,wpf,Wpf,如何将simpleImage的源设置为bi?“BitmapImage.UriSource必须位于BeginInit/EndInit块中”不是必需的。BitmapImage有一个接受Uri参数的构造函数。因此,您只需编写simpleImage.Source=newBitMapImage(新Uri(…)“BitmapImage.UriSource必须位于BeginInit/EndInit块中”不是必需的。BitmapImage有一个接受Uri参数的构造函数。因此,您只需编写simpleImage.S

如何将simpleImage的源设置为bi?

“BitmapImage.UriSource必须位于BeginInit/EndInit块中”不是必需的。BitmapImage有一个接受Uri参数的构造函数。因此,您只需编写
simpleImage.Source=newBitMapImage(新Uri(…)“BitmapImage.UriSource必须位于BeginInit/EndInit块中”不是必需的。BitmapImage有一个接受Uri参数的构造函数。因此,您只需编写
simpleImage.Source=newBitMapImage(新Uri(…)
// Create the image element.
Image simpleImage = new Image();    
simpleImage.Width = 100;
simpleImage.Margin = new Thickness(5);

// Create source.
BitmapImage bi = new BitmapImage();
// 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;
var uri = new Uri(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg");
var bi = new BitmapImage(uri);
simpleImage.Source = bi;