Strange behavior of ImageBrush Border Background in WPF

29 Views Asked by At

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:

  1. Initial state:

enter image description here

  1. Click on button 1 (set): image t1.jpg is set as background of myBorder. That is OK.

enter image description here

  1. Click on button 2 (change file): content of file t1.jpg is replaced with content of file t2.jpg, names remain unchanged.

  2. Click on button 1 (set) again: now myBorder should have different background, because content of file t1.jpg has been replaced with content of file t2. jpg. But nothing happened! Why?

0

There are 0 best solutions below