How to change Border Background image programmatically - wpf

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;

Related

Crop an Image in WPF

Give an image:
Image tileSet = new Image();
tileSet.Source = new BitmapImage(new Uri(#".."));
How can i cropt it, defining a rectangle area?
You can use CroppedBitmap for that
var fullBitmap = new BitmapImage(new Uri(#".."));
tileSet.Source = new CroppedBitmap(fullBitmap, new Int32Rect(0, 0, 100, 100));
To build a little on dkozl's answer above,
I found the DPI of the original image to crop and the DPI of the display were creating an "offset" of where I was intending to crop the original image.
The "offset" can be corrected by matching the image Width and Height in pixels with the DPI of the screen, instead of letting the Image element to it's own automatic sizing.
You do this by setting the Stretch to None, and the Width & Height of the image as below:
<Image
x:Name="imageToCrop"
Stretch="None"
Width="{Binding Source.PixelWidth,RelativeSource={RelativeSource Self}}"
Height="{Binding Source.PixelHeight,RelativeSource={RelativeSource Self}}"/>

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 can I set a background image in code?

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

Silveright ScrollViewer with Image and ScaleTransform

I have the following xaml.
<ScrollViewer HorizontalAlignment="Stretch" Margin="107,0,0,0" Name="scrollViewer1" VerticalAlignment="Stretch" HorizontalScrollBarVisibility="Visible">
<Image Name="image1" Stretch="None" MouseWheel="image1_MouseWheel" RenderTransformOrigin="0,0">
</Image>
</ScrollViewer>
An the following code behind.
// initialise.
private TransformGroup group = new TransformGroup();
private ScaleTransform st = new ScaleTransform();
group.Children.Add(st);
image1.RenderTransform = group
// mouse event.
TransformGroup group = (TransformGroup)image1.RenderTransform;
ScaleTransform scale = (ScaleTransform)group.Children.Last();
double zoom = e.Delta > 0 ? .2 : -.2;
scale.ScaleX += zoom;
scale.ScaleY += zoom;
How do I get the scroller to take into account that the image is now a different size.
The scroll bars remain the same size, and I cannot work out how to change them.
Thanks
You need the LayoutTransformer from the Silverlight Toolkit. Instead of setting a RenderTransform on your Image, put it inside a LayoutTransformer.
have you tried calling InvalidateScrollInfo on the scrollviewer?

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