Reputation: 143
I have a method that is called continuously to change the source of an image on a window using WPF. It is working fine, but I am definitely not trained in this and just starting out.
private void UpdateLBUImage(int val)
{
if (val == 1)
{
//Bitmap Creation
var gc = new Uri("pack://application:,,,/Images/check.png");
var gcb = new BitmapImage(gc);
StatusImage.Source = gcb;
}
else
{
//Bitmap Creation
var rx = new Uri("pack://application:,,,/Images/redx.png");
var rxb = new BitmapImage(rx);
StatusImage.Source = rxb;
}
}
My question is, am I recreating the bitmap image over and over? Is that bad or will it cause problems in my app?
Upvotes: 0
Views: 660
Reputation: 163
mm8 says truth)
But, why don't you want do smth like this:
<Image>
<Style TargetType="Image">
<Setter Property="Source" Value="path1"/>
<Style.Triggers>
<DataTrigger Binding="{Binding SomeBoolFlag}" Value="True">
<Setter Source="path2"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image>
Upvotes: 0
Reputation: 169390
You are creating a new BitmapImage
each time your method is called. If the Uris don't change, you could define two static fields to prevent this from happening:
private static readonly BitmapImage gcb = new BitmapImage(new Uri("pack://application:,,,/Images/check.png"));
private static readonly BitmapImage rxb = new BitmapImage(new Uri("pack://application:,,,/Images/redx.png"));
private void UpdateLBUImage(int val)
{
if (val == 1)
{
StatusImage.Source = gcb;
}
else
{
StatusImage.Source = rxb;
}
}
Then there will only be two BitmapImage
objects created regardless of how many times the UpdateLBUImage
method is called.
Upvotes: 2