C# 在代码后面设置WPF图像控件的源代码

C# 在代码后面设置WPF图像控件的源代码,c#,wpf,wpf-controls,bitmapimage,C#,Wpf,Wpf Controls,Bitmapimage,最近我一直在努力设置WPF图像控件的源代码 我试过这样的方法: Image.Source = new BitmapImage(new Uri(@"/hrc.Hands;component/Images/BlueFolder.png", UriKind.RelativeOrAbsolute)); 但它似乎不起作用,thorws对象引用未设置为对象异常的实例。以下是我的图像在层次结构中的路径截图: 您可以使用 var imageSource = new Uri(@"pack://applicati

最近我一直在努力设置WPF图像控件的源代码

我试过这样的方法:

Image.Source = new BitmapImage(new Uri(@"/hrc.Hands;component/Images/BlueFolder.png", UriKind.RelativeOrAbsolute));
但它似乎不起作用,thorws对象引用未设置为对象异常的实例。以下是我的图像在层次结构中的路径截图:

您可以使用

var imageSource = new Uri(@"pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Images/BlueFolder.png", UriKind.Absolute);
Image.Source = imageSource;
另外,确保BlueFolder.png设置为
资源
。右键单击图像,然后选择属性来执行此操作。

您可以使用

var imageSource = new Uri(@"pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Images/BlueFolder.png", UriKind.Absolute);
Image.Source = imageSource;

另外,确保BlueFolder.png设置为
资源
。右键单击图像,然后选择
Properties
执行此操作。

可能值得注意的是,这被称为a,或者更准确地说是a。可能值得注意的是,这被称为a,或者更准确地说是a。