I have border, 2 buttons and 2 jpg files.
This is code of my first button:
private void Button1_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri(@"C:\Users\abc\Documents\t1.jpg", UriKind.Relative);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = uri;
bi.EndInit();
ImageBrush ib = new ImageBrush(bi)
{
Stretch = Stretch.UniformToFill,
AlignmentX = AlignmentX.Center,
AlignmentY = AlignmentY.Center
};
myBorder.Background = ib;
}
This is code of my second button:
private void Button2_Click(object sender, RoutedEventArgs e)
{
File.Copy(@"C:\Users\abc\Documents\t2.jpg", @"C:\Users\abc\Documents\t1.jpg", true);
}
What I am doing:
- Initial state:
- Click on button 1 (set): image
t1.jpg
is set as background ofmyBorder
. That is OK.
Click on button 2 (change file): content of file
t1.jpg
is replaced with content of filet2.jpg
, names remain unchanged.Click on button 1 (set) again: now
myBorder
should have different background, because content of filet1.jpg
has been replaced with content of filet2. jpg
. But nothing happened! Why?