Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在运行时将窗口背景设置为驱动器中的图像_C#_Wpf_Xaml - Fatal编程技术网

C# 在运行时将窗口背景设置为驱动器中的图像

C# 在运行时将窗口背景设置为驱动器中的图像,c#,wpf,xaml,C#,Wpf,Xaml,图像在designer中显示,但在vs2013中作为调试运行时会出现错误: 另外,如果exe直接与同一文件夹中的图像一起运行 information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '12' and line position '10'. 错误是因为找不到图像。在xaml视图中,当悬停图像时,会显示: projec

图像在designer中显示,但在vs2013中作为调试运行时会出现错误: 另外,如果exe直接与同一文件夹中的图像一起运行

information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '12' and line position '10'.
错误是因为找不到图像。在xaml视图中,当悬停图像时,会显示:

project file expected in c:\user\bsienn\docs\vs2013\project\wpf1\wpf1\image1.jpg
尽管pic确实在这条道路上,并且是可用的

我想添加一个图像作为表单的背景,我不想在资源中添加图像,因为我想在需要时更改图像。我已将带有exe文件的图像放置在bin/debug和主应用程序文件夹(wpf1/image1.jpg和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

应用程序结构:
app.exe
图1.jpg
期望的结果,形式与背景图像

将图像放入输出文件夹不会使其可用于XAML

您需要
在项目中添加图像
,并将其设置为
构建操作
资源


右键单击project->Open Properties->Set Build Action to Resource中添加的图像。

这将根据需要执行

XAML:

AppDomain.CurrentDomain.BaseDirectory:
返回运行应用程序的当前工作目录,即c:\users\admin\Desktop\

我知道如何操作,但我不想在资源中添加图像,因为我希望在需要时更改图像。因此,我需要从可执行路径获取图像,图像所在的路径我担心,如果不将图像作为资源添加到project下,这是不可能的
<Window.Background>
    <ImageBrush x:Name="MainFormBgrImg"/>
</Window.Background>
BitmapImage bitimg = new BitmapImage();
bitimg.BeginInit();
bitimg.UriSource = new Uri(@""+AppDomain.CurrentDomain.BaseDirectory+"backgroundImg.jpg", UriKind.RelativeOrAbsolute);
bitimg.EndInit();
MainFormBgrImg.ImageSource = bitimg;