在 WPF 中显示构建操作设置为 'content' 的图像

Displaying images with build action set to 'content' within WPF

我在 WinForms 中有一个主项目,我在该项目中显示图片,构建操作设置为“content”。我有一个 WPF window 在我的 WinForms 项目中工作,目前唯一不起作用的是显示构建操作设置为 'content[= 的图像31=]' 在我的 WPF window 中。如果我将构建操作更改为“resource”,我就可以很好地显示图像。不幸的是,这确实破坏了我的 WinForms 项目中的功能。

那么有什么方法可以在 WPF 中显示图像并将构建操作设置为“content”?当然不会破坏我的 WinForms 项目,因为我在 WinFormsWPF.

中显示相同的图像

编辑: 我找到了解决方案,

this answer combined with this 答案中显示了最终对我有用的技巧。

我从here中选择了解决方案2a:

Image will be in a SubFolder called "Resources" and .exe file will be small

Source="pack://application:,,,/Resources/example.png"
Build Action = Content
Copy to output directory = Copy if newer

然后我做了:

ImageSource imageSource = new BitmapImage(new Uri("pack://application:,,,/Resources/example.png"));
image.Source = imageSource;