ImageBrush throws exception when trying to set Window background - wpf

I'm trying to set the background of my WPF window to an image but I'm getting this exception when I try to run it:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '8' and line position '10'.
I don't want to add the image to the project, as I would like to be able to change the image at runtime. My intention is to use databinding to set the background picture during start-up once I have this bit working.
Source Code:
<Window x:Class="ColinsTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Colin's Test Application"
WindowStyle="None"
WindowState="Maximized">
<Window.Background>
<ImageBrush
ImageSource="DeLaRue-Background.jpg"/>
</Window.Background>
<Grid></Grid>
</Window>
Any Ideas? Thanks

Wrong. It should be set to CONTENT not Resource.
Include in your project (use the file explorer)
Right click on the image > Properties > Advanced.
Build Action: Content
Copy to Output Directory: Copy always.

Your image's build action is probably set to Content, change it to Resource
Set the item (your image file) to be a Resource. (In Visual Studio) Right click,Properties,Build Action,Resource

I had the same problem with
<Button>
<Button.Background>
<ImageBrush ImageSource="/Resources/icon_done.png">
</Button.Background>
</Button>
I used / before Resources and it work correctly, but i used solution with properties image ->Properties->Build to Resources.
These two actions solved my issue.

Add the forward "/" to the location
Then the whole ImageSource string should look like
<ImageBrush ImageSource="/Resources/Image1.png" Stretch="None" />

Related

RibbonSplitButton cannot locate resource

I have a RibbonSplitButton that contains a SmallImageSource like so:
<UserControl x:Class="myProgram/toolbars/MainToolbar">
<RibbonSplitButton SmallImageSource="/Images/Undo.png"/>
<!-- more buttons-->
</UserControl>
I try to use this in another file (main):
<UserControl x:Class="myProgram/main"
xmlns:toolbar="clr-namespace:myProgram.toolbars">
<toolbar:MainToolbar/>
</UserControl>
I get the error message in main:
Cannot locate resource 'images/undo.png'
However, when I switch to a normal button:
<Button>
<Image Style="{StaticResource buttonstyle}">
<Image.Source>
<BitmapImage DecodePixelWidth="40" UriSource="/Images/Undo.png"/>
</Image.Source>
</Image>
</Button>
the error is gone. I have tried using Pack URIs, changing the Build Action to Resource and Embedded Resource, as well as Clean Soultion, Rebuild Solution, Restarting Visual Studios (rinse and repeat). I image this has something to do with the RibbonSplitButton's image source, but I have no idea. The program runs fine (the Undo.png image shows and works perfectly), it's just annoying to have the error in my developer. Anyone have suggests on how to get rid of the error? (Note, this is a simplified folder structure, for example purposes).
EDIT
For anyone who ends up here at some point in the future, I found that this answer worked.

Background image from a local file/resource

I need to set the background image from a path like this: "Data/locale/skin/skin.jpg", and i've tried in xaml to set the image as a resource... but i failed.
So, my question is:
How can i set the background image for my wpf application by c# code / xaml ?
What i've tried:
<Window Background="{StaticResource MyImageBrush}"> ...
<Window.Resources >
<ImageBrush x:Key ="MyImageBrush" ImageSource ="Data/locale/ro-RO/skin/Login/login.jpg" />
</Window.Resources >
If you have added your images to a folder named Data in the root of your project using the Add Existing Item command in Visual Studio, then you can reference your images using a simple format:
/ApplicationName;component/FolderName/ImageName.type
In your case, it would be like this (where you replace ApplicationName with the actual name of your application):
<Window Background="{StaticResource MyImageBrush}"> ...
<Window.Resources>
<ImageBrush x:Key="MyImageBrush" ImageSource="/ApplicationName;component/Data/
locale/ro-RO/skin/Login/login.jpg" />
</Window.Resources>
If you didn't add your images like I mentioned above, then just make sure that their Build Action is set to Resource in their property windows in Visual Studio.

Silverlight : how to put a simple image?

I have troubles with element.
Well, I think my path should be wrong but I dont find the problem.
Here is my simple code :
<UserControl x:Class="videoplayer.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="Black">
<Image x:Name="btnPlay" Source="cat.jpg" />
</Grid>
</UserControl>*
I have put the file cat.jpg in the same directory that the .xap file, in the parent directory too, and again in the parent directory, but i still dont see any cat when I launch the application ! :(
Only the black grid ...
Someone see the mistake ?
Thanks !
ps : it's silverlight 2 sdk
You have to include the file in your project as a source. then in the property inspector select the image for the Source attribute.
The image will need to have a build-action setting of Resource.
From within Visual Studio:
Select the file,
open the Properties view,
select Resource from the Build Action drop-down.

More Issues Opening Window WPF

I'm using VB.net and WPF 4.
I cannot seem to open a new window in WPF consistantly. It will work one time, and then the next time I execute the same script, it throws the following exception:
XAMLParseException occured 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '4' and line position '208'. {Cannot evaluate expression because the code of the current method is optimized.}
Here is the script:
Dim Window As Window = New GAME_WINDOW
Window.Show()
Yes, "GAME_WINDOW" is a legitimate WPF window. Here are the first four lines of XAML code for the window I'm trying to open:
<Window x:Class="GAME_WINDOW"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Operation SpyRat: Word4Word" Icon="/VBP-WORD4WORD;component/alicia.ico" WindowState="Maximized" Background="Black" xmlns:my="clr-namespace:VBP_WORD4WORD">
I suspect the problem is the window Icon specification in the XAML. Try removing the Icon="..." from your XAML, and see if it corrects the issue.
If so, you'll need to make sure this is specified in proper Pack URI format. Most likely, this would be:
Icon="pack://application:,,,/component/alicia.ico"

Pack URI and path not resolving image in WPF

I have the following directory structure
Project
\Images
+view.png
control.xaml
and in the control I have a button defined by the following XAML:
<Button Click="Search"
Grid.Column="1"
Margin="0,5,5, 0"
HorizontalAlignment="Right">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Image Source="pack://application:,,,/images/view.png"
Width="16"
Height="16"
ToolTip="Search"
Cursor="Hand"
Opacity="0.8" />
</ControlTemplate>
</Button.Template>
</Button>
However, neither this pack URI method nor the "/images/view.png" is working. As I understand it, this is the same issue this question raises. However, I get the same error. The confusing thing is that in designer in Visual Studio 2008, the image renders correctly, but on the call to the InitializeComponent() call, I get:
Cannot convert string 'pack://application:,,,/images/view.png' in attribute 'Source' to object of type 'System.Windows.Media.ImageSource'. Cannot locate resource 'images/view.png'. Error at object 'System.Windows.Controls.ControlTemplate' in markup file 'RecapSpecEditControl;component/modaltreadgroupdatadialog.xaml' Line 61 Position 40.
I thought that maybe there was a namespace that I had to declare but according to the msdn site I believe I don't have to do anything like that.
I actually got this to work, but had to set my source to "/ProjectName;component/images/view.png" Because I have the ProjectName as a referenced assembly this is then the same as the Path: portion at the msdn page that I referenced in the question.
Set the Build Action for 'view.png' to Resource instead of Content and this problem should go away. I was able to reproduce your problem this way and it works correctly when set as a Resource.
Xaml.VB
Call the Image from Application folder and Design Page
Private Sub LoadImages()
Dim strUri As String
strUri = AppDomain.CurrentDomain.BaseDirectory() & "\NavigationImages\settingsicon.png"
Image2.Source = New BitmapImage(New Uri(strUri))
End Sub
Page load in Xaml.VB
Call LoadImages()
Xaml Design Page
Image Name="Image2"Height="32" HorizontalAlignment="Left"

Resources