2013-12-22 74 views
0

图像显示在设计师,但驱动图像时,如调试在vs2013运行它给人的错误: 此外,如果exe文件直接在同一文件夹中的图像跑。设置窗口背景从上运行

information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '12' and line position '10'. 

错误是bcoz它找不到图像。在xaml视图中,当鼠标悬停在图像上时,它说:

project file expected in c:\user\bsienn\docs\vs2013\project\wpf1\wpf1\image1.jpg 

尽管图片确实在那条路径上并且可用。

我想添加一个图像作为窗体的背景,我不想在资源中添加图像,bcoz我希望在需要时更改图像。我已经把exe文件放在图像上,并试图将它放在bin/debug和主应用程序文件夹(wpf1/image1.jpg andalso wpf1/wpf1/image1.jpg)中。

这里是XAML代码,请指导

<Window.Background> 
    <ImageBrush ImageSource="image1.jpg"/> 
</Window.Background> 

App structure: 
app.exe 
image1.jpg 
Desired outcome, form with background image 

回答

1

根据需要

XAML这样做

BitmapImage bitimg = new BitmapImage(); 
bitimg.BeginInit(); 
bitimg.UriSource = new Uri(@""+AppDomain.CurrentDomain.BaseDirectory+"backgroundImg.jpg", UriKind.RelativeOrAbsolute); 
bitimg.EndInit(); 
MainFormBgrImg.ImageSource = bitimg; 

AppDomain.CurrentDomain.BaseDirectory:从哪里哪里的应用程序从跑 返回当前工作目录,即c:\用户\管理\桌面\

0

输出文件夹中把图像不会将其提供给您的XAML。

您需要add image in your project并将它的Build Action设置为Resource

右键单击添加的图像项目 - >打开属性 - >设置生成操作的资源。

<Window.Background> 
    <ImageBrush x:Name="MainFormBgrImg"/> 
</Window.Background> 

后面的代码:

+0

我知道该怎么做,但我不想在资源中添加图像,bcoz我希望在需要时更改图像。所以我需要从可执行路径中获取图像所在的位置 – ADi

+0

我很害怕,没有在项目中添加图像作为资源是不可能的。 –

+0

omg :(这太奇怪了,它不可能,但是4时间 – ADi