Question

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 >
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top