当我启动项目时,图像不会加载

时间:2014-05-14 16:53:00

标签: c# wpf

我在窗口中添加了一个图像,当我启动项目时,图像无法加载。

XAML代码:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Image HorizontalAlignment="Left" Height="188" Margin="88,64,0,0" VerticalAlignment="Top" Width="288">
            <Image.Source>
                <BitmapImage UriSource="image1.png"/>
            </Image.Source>
        </Image>
        <Rectangle Fill="#FF2424C7" HorizontalAlignment="Left" Height="100" Margin="338,24,0,0" Stroke="Black" VerticalAlignment="Top" Width="125"/>
    </Grid>
</Window>

我可以在Visual Studio设计器中看到图像:

Screenshot of visual studio showing that the image is shown in designer.


图像文件存在于解决方案目录中:

Screenshot of Windows Explorer showing that the file exists in the solution directory.

1 个答案:

答案 0 :(得分:3)

确保将图像复制到输出文件夹。在Visual Studio中(我只有2010作为参考),查看图像文件的属性并确保Copy to Output Directory设置为&#34;如果更新则复制&#34;。

Screenshot of Visual Studio 2010 properties window showing the 'Copy to Output Directory' property.

您可以通过查看bin目录来检查是否正在复制图像。这应该是项目目录下的bin\Debugbin\Release