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.
Related
I am working on a UserControl in a Silverlight project in visual studio 2010.
This user control uses Labels as follows:
<sdk:Label .../>
-or-
<sdk:Label>
singleObject
</sdk:Label>
-or-
<sdk:Label>stringContent</sdk:Label>
as described here
However, when I create a WPF project link it to Silverlight Library Project:
(by adding existing items (Add Link) to cs classes and the .xaml file defining the control)
I get the following error in build:
The tag 'Label' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk'. Line 49 Position 18. myfile.xaml 49 18 MyLibraryWPF
This doc shows that labels in WPF are declared without the sdk:
like this:
<Label>
Content
</Label>
yet, I cannot change that in my Silverlight project. It will be a real waste to create a whole new project and duplicate the code to make it work.
How to make the Labels work with both Silverlight and WPF projects?
Here is a piece of Code in xaml that can be shared by WPF and Silverlight and looks like label with Background coloe:
<Border Margin="3,0,3,3" Background="#FF00B050">
<TextBlock Text="txt122" FontSize="13" FontWeight="SemiBold" Height="21" Name="label15" Width="133" ></TextBlock>
</Border>
This means that a Label cannot be used in a shared XAML.
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" />
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.
The image in question is located in a ControlTemplate inside of a ResourceDictionary similar to this (various details removed for clarity):
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<ControlTemplate x:Key="ImageTestTemplate" TargetType="ImageTest">
<Grid>
<Image Source="/MyAssembly;Component/Images/MyImage.png"/>
</Grid>
</ControlTemplate>
</ResourceDictionary>
When I view the control in Expression Blend the image shows up just fine, but when I run the app the image does not show up. If I drop the same image into a UserControl it also shows up just fine, so the problem has something to do with using the template.
Is there some other way I should be loading the image resource inside the template?
I don't think you need the leading '/'. The base resource path I'm using for a project is "IMLPrototype2;component/Model/Sounds/" in SL 3 & 4.
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"