通过 WPF 中的 XAML 代码加载外部图像?

发布于 2024-08-29 19:26:23 字数 337 浏览 2 评论 0原文

我的 images 文件夹中的 WPF exe 文件旁边有一个图像 lock.png。 现在,我要将其作为图像加载到 WPF 项目中,我使用了以下 XAML 代码:

<Image Stretch="Fill" Source="pack://siteoforigin:,,,/images/lock.png" />

它可以工作,但 Expression BlendVisual Studio 不行。当我从事该项目时不会显示它。
在这些情况下我们如何展示外部图像呢?

I have an image lock.png beside of my WPF exe file in the images folder.
Now, I'm gonna load it into the WPF Project as an image, I've used the following XAML code:

<Image Stretch="Fill" Source="pack://siteoforigin:,,,/images/lock.png" />

It works, but Expression Blend or Visual Studio doesn't show it when I'm working on the project.
How can we show external images in these situations?

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

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

发布评论

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

评论(7

蓝戈者 2024-09-05 19:26:23

尝试动态加载图像。这应该在 xaml 上:

<Image Stretch="Fill" Name="MyImage" />

并且在代码后面。在 Window_Loaded 或 Window 构造函数中:

if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "images/lock.png"))
            {
                Uri uri = new Uri(AppDomain.CurrentDomain.BaseDirectory + "images/lock.png", UriKind.RelativeOrAbsolute);
                MyImage.Source = BitmapFrame.Create(uri);
            }

Try to load your image dynamically. This should be on xaml:

<Image Stretch="Fill" Name="MyImage" />

And this in code behind. On Window_Loaded or in Window constructor:

if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "images/lock.png"))
            {
                Uri uri = new Uri(AppDomain.CurrentDomain.BaseDirectory + "images/lock.png", UriKind.RelativeOrAbsolute);
                MyImage.Source = BitmapFrame.Create(uri);
            }
雪花飘飘的天空 2024-09-05 19:26:23

使用如下格式:
项目;组件/ImagePath

例如

<Image Source="ImageDemo;component/Images/ISIBAR.png" Name="custLogo"/>

其中ImageDemo是项目名称,Image/ISIBAR.png是项目内部路径

Use format like:
Project;component/ImagePath

e.g.

<Image Source="ImageDemo;component/Images/ISIBAR.png" Name="custLogo"/>

Where ImageDemo is the project name, Image/ISIBAR.png is the path inside project

梨涡 2024-09-05 19:26:23

如果图像是相对于您的 EXE 位置,则只需执行“

<Image Source="Images\lock.png" />

如果图像不是相对的”,那么您将遇到更大的问题。仅当您实际将资源“打包”到程序集中时,pack 语法才有用。

松散图像和 Blend 的问题在于,Blend 将您的 exe 托管在它控制的临时目录中,并查找相对于该临时目录的图像,这将破坏您所依赖的任何路径。

If the image is relative to your EXE location just do

<Image Source="Images\lock.png" />

If the image isn't relative then you have a larger problem. pack syntax only useful if you are actually "packing" the resource into your assembly.

The problem with loose images and Blend is that Blend hosts your exe in a temp directory that it controls and looks for images relative to that temp directory, which will screw any pathing you are depending on.

你的他你的她 2024-09-05 19:26:23

我有同样的问题。

确保映像构建操作设置为资源。 (右键单击图像,然后转到属性,将构建操作设置为资源)

而不是 siteoforigin

此外,使用应用程序权限source
https://stackoverflow.com/a/18175145/2672788

I had same question.

Make sure image build action is set to Resource. (right click on an image and then go to properties, set build action to resource)

Also, instead of siteoforigin use application authority

source:
https://stackoverflow.com/a/18175145/2672788

吹泡泡o 2024-09-05 19:26:23

可能更容易:

<Image x:Name="ImageObject" Source="file:///C:\\1.jpg"/>

记住反斜杠!

Could be easier:

<Image x:Name="ImageObject" Source="file:///C:\\1.jpg"/>

Remember the backslashes!

丶情人眼里出诗心の 2024-09-05 19:26:23

很简单,您的图像未显示,因为应用程序运行后没有读取它。

解决此问题的一种快速方法是手动将图像从物理文件夹拖放到应用程序中的文件夹中。一旦到达那里,应用程序就能够读取它。

It is very simple, your image is not displaying because it is not being read by the application after you run it.

A quick way to get around this is by manually dropping the image from the physical folder to the folder in the application. Once it is there, the application will be able to read it.

岁月苍老的讽刺 2024-09-05 19:26:23

您的主要 IDE 是 Visual Studio 吗?
如果是,为什么要手动执行此操作?在“属性”窗口中,您可以浏览要与图像组件一起使用的图像的方式

Is your primary IDE Visual Studio?
If yes, why do this manualy? In Propeties window you can just browse way to image you want to use with your Image component

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