ImageBrush (Canvas) 路径不工作

ImageBrush (Canvas) path not working

我正在为交互式应用程序开发 WPF。当我将图像路径从解决方案中包含的文件夹分配给 ImageBrush (Canvas) 时,它不起作用,它转到 bin/debug/.. in WPF。但是当我从目录分配路径时,它工作得很好。以下是问题截图:

screenshot

以下是我编写的无法运行的代码片段:

ImageBrush myBrush = imgContent1;
myBrush.ImageSource = new BitmapImage(new Uri(@"Informatin_Concept_100.jpg", UriKind.Relative));
canContent1Info.Background = myBrush;

但是当我从目录分配图像时,以下代码片段有效:

C = new System.IO.DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;
imgContent1.ImageSource = new BitmapImage(new Uri(C + "\" + "Content1\Default\Informatin_Concept_100.jpg", UriKind.Absolute));

不知道哪里出错了?

使用 WPF Resource File Pack URI 从程序集资源加载 BitmapImage。 Visual Studio 项目中图像文件的 Build Action 必须设置为 Resource

myBrush.ImageSource = new BitmapImage(
    new Uri("pack://application:,,,/Informatin_Concept_100.jpg"));