2013-12-22 72 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