簡體   English   中英

在運行時將窗口背景設置為驅動器中的圖像

[英]Set Window background as image from drive on runtime

圖像在設計器中顯示,但是在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

盡管圖片確實在那條路徑上並且可用。

我想添加圖像作為表單的背景,我不想在資源中添加圖像,我想在需要時更改圖像。 我已將圖像與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

這將按需完成

XAML:

<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;

AppDomain.CurrentDomain.BaseDirectory:返回應用程序運行所在的當前工作目錄,即c:\\ users \\ admin \\ Desktop \\

將圖像放在輸出文件夾中不會使其可用於XAML。

您需要add image in your project ,並將其Build Action設置為Resource

右鍵單擊項目中添加的圖像->打開屬性->將構建操作設置為資源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM