图像未在C#WPF中显示 [英] Image not displaying in C# WPF

查看:72
本文介绍了图像未在C#WPF中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试学习C#,但是遇到很多问题.我正在尝试在WPF中显示图像,但是由于某种原因,该图像将无法显示!它出现在Visual Studio编辑器上,但是当我运行该应用程序时它被隐藏了.

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.

这是我的一些代码:

这就是我试图显示图像的方式:

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"/>

我也尝试过使用它:

Source="pack://application:,,,/image.jpg"

感谢您的帮助!

推荐答案

在您的项目中:

  1. 创建一个名为"img"的文件夹,右键单击它,然后选择添加现有项,将图像添加到文件夹
  2. 转到添加的图像的属性,将Build Action设置为Resource,将Copy To Output Directory设置为Copy if newer.
  1. Create a folder say "img", right click on it and select add existing item add image to folder
  2. Go to properties of the added image, set Build Action as Resource and Copy To Output Directory as Copy if newer.

对我有用.

在XAML中

<Image HorizontalAlignment="Left"  Name="MyImg" Height="80" Margin="273,147,0,0" 
    VerticalAlignment="Top" Width="100" Source="/img/Desert.jpg"/>

这篇关于图像未在C#WPF中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆