how can I set a background image in code? - wpf

If I want to set a image as background on a textBox I can use this code in the axml:
<Grid>
<Grid.Background>
<ImageBrush ImageSource="MyImage.jpg" />
</Grid.Background>
<TextBlock Text="Some Text" />
</Grid>
However, I am creating a TextBlock in code, I amtrying this:
TextBox myTextBox = new TextBox();
But in this way I don't know how to access to the ImageBrush property.
Which is the way to add a background in code?
Thank so much.

Provided that MyImage.jpg is a file in the application's current folder, you could write
myTextBox.Background = new ImageBrush(new BitmapImage(new Uri("MyImage.jpg")));
If it's a Resource File, you would have to use a Resource File Pack URI:
myTextBox.Background =
new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/MyImage.jpg")));

Related

WPF assigning border background from image resource

I'm trying to simulate a grid inventory system. I have this grid with some rows and cols. I have a resource that is an image. The error I'm getting is:
Cannot implicitly convert type 'System.Windows.Controls.Image' to 'System.Windows.Media.Brush'
If I change my Image cast to ImageBrush then the project compiles but the exe crashes right away.
<Grid x:Name="MasterGrid" Margin="0">
<Grid.Resources>
<Image x:Key="notepad" Source="notepad_16x16.jpg" />
</Grid.Resources>
// create a border and set it's background image
Border border = new Border();
border.Visibility = System.Windows.Visibility.Visible;
var img = (Image)MasterGrid.FindResource("notepad");
border.Background = img;
// add the border to the grid
Grid.SetRow(border, 0);
Grid.SetColumn(border, 1);
Grid.SetRowSpan(border, 1);
Grid.SetColumnSpan(border, 1);
InvGrid.Children.Add(border);
The border.Background is expecting a brush, and you're populating it with an image. You need to create an ImageBrush from the Image resource
border.Background = new ImageBrush((BitmapImage)FindResource("notepad"));
Your image resources should be defined as follows:
<Grid.Resources>
<BitmapImage x:Key="notepad" UriSource="images/notepad_16x16.jpg" />
</Grid.Resources>
<Grid.Background>
<ImageBrush ImageSource="C:\... your path to the image\notepad_16x16.jpg"/>
</Grid.Background>
this works for me without setting borders.

How to change Border Background image programmatically

I am creating a media player app in WPF c#.
I am using Media Element to do this.
Anyways, I have used <Border> </Border> to add border some places.
<Border Name="hej1">
<Border.Background>
<ImageBrush ImageSource="Images\music.png" Stretch="None"/>
</Border.Background>
<MediaElement ..../>
</Border>
I want to change the ImageSource to some other picture programmatically, how to do that?
I have tried but no success.
So for every song the image in <ImageBrush ImageSource="Images\music.png" is changed.
Thanks in advance
Shafi
Assign a Name to the ImageBrush:
<ImageBrush x:Name="imageBrush" ImageSource="Images\music.png" Stretch="None"/>
Then use the named member in code:
var filename = #"Images\title.png";
imageBrush.ImageSource = new BitmapImage(new Uri(filename, UriKind.Relative));
Or simply cast the value of the Border's Background property to type ImageBrush:
var imageBrush = (ImageBrush)hej1.Background;
var filename = #"Images\title.png";
imageBrush.ImageSource = new BitmapImage(new Uri(filename, UriKind.Relative));
BitmapImage img = new BitmapImage(new Uri(#"Images\myimage.png"));
ImageBrush image = new ImageBrush();
image.ImageSource = img;
Border.Background =image;

Access icon resources through URI VB.NET

I have a WPF VB.NET application and I want to use an icon embedded in the applications resources as a menu icon. So far I have this code (in the window's initialized event):
MenuItem.Icon = New Image() With {.Source = New BitmapImage(New
Uri("Resources\Icon.ico", UriKind.Relative))}
And the icon is still not displayed, any ideas?
The problem is your URI. If you set it in code behind, you must write the full WPF Pack URI. You must also set the Build Action
of the icon file to Resource (the default value for icons is None).
MenuItem.Icon = New Image() With
{
.Source = New BitmapImage(New Uri("pack://application:,,,/Resources/Icon.ico"))
}
When you specify the URI in XAML, the default ImageSource TypeConverter will add the pack://application:,,, part, and you could simply write
<Image Source="/Resources/Icon.ico"/>
Better option is building menu in XAML:
Create folder Images in your solution
Add image as Resources to Images directory (in my sample code: "Icon.ico")
In XAML you can use following code:
...
<MenuItem Header="Item1">
<MenuItem.Icon>
<Image Source="/Images/Icon.ico" Width="20" Height="20" />
</MenuItem.Icon>
</MenuItem>
Or if you want to do this in code-behind you can use following code instead of step 3:
MenuItem.Icon = New Image() With {.Source = New BitmapImage(New Uri("/Images/Icon.ico", UriKind.RelativeOrAbsolute))}

Loading external images in WPF

I am building an application using WPF for Windows 8 in VB.NET and I need to load several images from the disk.
I don't want to add the images as a reference to the project, I just want to load them as is.
I already studied the "Imaging Overview" at MSDN and several other articles over the internet but nothing solved my problem.
I used both XAML and code but nothing worked. I can't get the image presented.
Dim myimage As New Image
myimage.Width = 200
Dim bitmapimage As New BitmapImage()
bitmapimage.UriSource = New Uri("C:\Users\MyName\Documents\Database\image.jpg")
myimage.Source = bitmapimage
XAML code:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Image HorizontalAlignment="Left" Height="265" Margin="191,145,0,0" VerticalAlignment="Top" Width="660">
<Image.Source>
<BitmapImage UriSource="C:\Users\MyName\Documents\Database\image.jpg"/>
</Image.Source>
</Image>
</Grid>
When I load a referenced image (ie. Assets/Logo.png) it loads it correctly.
I also added all the necessary capabilities and declerations in the application manifest.
Your XAML looks correct. Maybe the image path is invalid. However it could be written simpler like this:
<Image HorizontalAlignment="Left" Height="265" Margin="191,145,0,0" VerticalAlignment="Top" Width="660"
Source="C:\Users\MyName\Documents\Database\image.jpg" />
The path string is automatically converted to an ImageSource instance.
In code you should use the BitmapImage constructor with Uri parameter. Otherwise you would have to call BeginInit and EndInit.
Dim uri As New Uri("C:\Users\MyName\Documents\Database\image.jpg")
Dim bitmapimage As New BitmapImage(uri)
myimage.Source = bitmapimage

How to assign a dynamic imagebrush resource to a stackpanel's background in code?

I currently define the background for a user control like this:
<UserControl.Background>
<ImageBrush ImageSource="{DynamicResource LeftMenuBackgroundImage}" />
</UserControl.Background>
How can I move this to code-behind, e.g.:
PSEUDO-CODE:
StackPanel sp = new StackPanel();
sp.Background = new ImageBrush(DynamicResource.GetResourceName("LeftMenuBackgroundImage"));
allow me to answer this one, got it to work like this:
in code:
StackPanel sp = new StackPanel();
sp.SetResourceReference(StackPanel.BackgroundProperty, "LeftMenuBackgroundImageBrush");
in resources:
<ImageBrush x:Key="LeftMenuBackgroundImageBrush"
ImageSource="{DynamicResource LeftMenuBackgroundImage}"/>
<ImageSource x:Key="LeftMenuBackgroundImage">Images/LeftMenuBackground.jpg</ImageSource>

Resources