如何将 WPF BitmapImage UriSource 属性设置为相对路径?

发布于 2024-09-08 13:34:59 字数 526 浏览 1 评论 0原文

我正在使用 BitmapImage 类在 WPF 中加载图像。当我为 UriSource 提供绝对路径时,我的代码可以完美运行,但当我尝试使用相对路径时,则不能。

我的 XAML 是:

<Image Width="50" Name="MemberImage" HorizontalAlignment="Left">
    <Image.Source>
        <BitmapImage DecodePixelWidth="50" UriSource="questionmark.jpg" />
    </Image.Source>
</Image>

Questionmark.jpg 添加到项目中,并将资源的构建操作和复制到输出目录设置为始终复制。我收到的错误是“文件 [文件名] 不是项目的一部分或其“构建操作”属性未设置为“资源””。当我对 UriSource 使用绝对路径时,这是有效的,但显然不行。

我应该如何在 XAML 中定义我的 UriSource?

I'm loading an image in WPF by using the BitmapImage class. My code works perfectly when I give an absolute path for the UriSource but not when I try and use a relative path.

My XAML is:

<Image Width="50" Name="MemberImage" HorizontalAlignment="Left">
    <Image.Source>
        <BitmapImage DecodePixelWidth="50" UriSource="questionmark.jpg" />
    </Image.Source>
</Image>

The questionmark.jpg is added to the project with a Build Action of Resource and Copy to Output Directory set to Copy always. The error I get is "The file [file name] is not part of the project or its 'Build Action' property is not set to 'Resource'". This works when I use an absolute path for the UriSource but that obviously won't do.

How should I be defining my UriSource in the XAML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情感失落者 2024-09-15 13:34:59

我认为一旦图像位于资源中,您就不需要将其复制到输出目录。

正确的指定方法是

<BitmapImage 
    x:Key  = "Logo"
 UriSource = "pack://application:,,,/ApplicationNamespace;component/Images/App/image.png"  
/>

ApplicationNamespace 替换为您的应用程序命名空间

,并将

Images/App/image.png 替换为您在项目中的图像路径

I don't think you need to copy the image to output directory once it's in resource.

Correct way to specify is

<BitmapImage 
    x:Key  = "Logo"
 UriSource = "pack://application:,,,/ApplicationNamespace;component/Images/App/image.png"  
/>

Just replace

ApplicationNamespace with your application namespace

and

Images/App/image.png with your image path in the project

陪你搞怪i 2024-09-15 13:34:59

属性中具有以下选项的图像文件
构建操作=内容
复制到输出目录=如果较新则复制

<BitmapImage x:Key="QuestionMark" UriSource="pack://siteoforigin:,,,/questionmark.png"/>

参考:
Xaml - 位图 UriSource - 绝对路径有效,相对路径无效,为什么?

Image files with the following options in properties
Build Action=Content
Copy to Output Directory=Copy if newer

<BitmapImage x:Key="QuestionMark" UriSource="pack://siteoforigin:,,,/questionmark.png"/>

Reference:
Xaml - Bitmap UriSource - absolute path works, relative path does not, why?

梦旅人picnic 2024-09-15 13:34:59

我无法在我的计算机上重现该问题:

  • 我通过在“项目”菜单中选择“添加现有项目”来添加 jpg,
  • 然后将其“构建操作”设置为“资源”,并将“复制到输出目录”设置为“始终”。
  • 我的 XAML 也是一样。

即使在移动 .exe 并重命名 jpg 之后,这里也能完美运行。一定有别的东西在咬你!

I cannot reproduce the problem on my computer:

  • I add the jpg by choosing Add existing item in the Project menu
  • I then set its Build Actio to Resource and Copy to Output directory to always.
  • My XAML is the same.

Works perfectly here, even after moving the .exe and renaming the jpg. Something else must be biting you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文