图像未在 C# WPF 中显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24049511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Image not displaying in C# WPF
提问by EthanBar
I have been trying to learn C# but am coming across a lot of problems. I am trying to display an image in WPF but for some reason, the image won't show! It appears on the Visual Studio editor but when I run the application it is hidden.
我一直在尝试学习 C#,但遇到了很多问题。我正在尝试在 WPF 中显示图像,但由于某种原因,图像不会显示!它出现在 Visual Studio 编辑器中,但是当我运行应用程序时它是隐藏的。
Here is some of my code:
这是我的一些代码:
This is how I'm trying to display the image:
这就是我试图显示图像的方式:
<Image HorizontalAlignment="Left" Height="100" Margin="273,147,0,0"
VerticalAlignment="Top" Width="100" Source="image.jpg"/>
I have also tried using this:
我也试过使用这个:
Source="pack://application:,,,/image.jpg"
Thanks for your help!
谢谢你的帮助!
回答by swapnil
In your project:
在您的项目中:
- Create a folder say "img", right click on it and select add existing item add image to folder
- Go to properties of the added image, set
Build ActionasResourceandCopy To Output DirectoryasCopy if newer.
- 创建一个文件夹说“img”,右键单击它并选择添加现有项目将图像添加到文件夹
- 转到累加图像的属性,设置
Build Action为Resource和Copy To Output Directory作为Copy if newer。
It worked for me.
它对我有用。
In XAML
在 XAML 中
<Image HorizontalAlignment="Left" Name="MyImg" Height="80" Margin="273,147,0,0"
VerticalAlignment="Top" Width="100" Source="/img/Desert.jpg"/>
回答by Christoffer Eriksson
Go to the properties for the image in Visual Studio and change "Build Action" to "Resource" and "Copy to Output Directory" to "Copy if newer".
转到 Visual Studio 中图像的属性,将“构建操作”更改为“资源”,将“复制到输出目录”更改为“如果更新则复制”。
I had to do a rebuild, but then it worked. Cred to swapnil.
我不得不进行重建,但后来成功了。信任交换。
回答by Harjeet Singh
please drag the image to a image source,it will be like this /img/image.jpg"/
请将图片拖到图片源,它会是这样的/img/image.jpg"/
<Image HorizontalAlignment="Left" Height="100" Margin="273,147,0,0"
VerticalAlignment="Top" Width="100" Source="/img/image.jpg"/>

