I have issue with set-up image programmatically on button. I have next WPF button:
<Button Name="MyBtn" x:FieldModifier="public" HorizontalAlignment="Center" VerticalAlignment="Center" Click="FlashOnOf_Click" Height="256" Width="256" BorderBrush="{x:Null}"/>
and try to set-up image on it with next cases:
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("ms-appx://Assets/light_bulb_off.png"));
brush.Stretch = Stretch.Fill;
MyBtn.Background = brush;
one more case:
MyBtn.Content = new Image
{
Source = new BitmapImage(new Uri("ms-appx://Assets/light_bulb_off.png")),
VerticalAlignment = VerticalAlignment.Center,
Stretch = Stretch.Fill,
Height = 256,
Width = 256
};
Also with no luck. Any ideas where i've made mistake?
You should set image like this:
private void btn_Click(object sender, RoutedEventArgs e)
{
btn.Content = new Image
{
Source = new BitmapImage(new Uri("/WpfApplication1;component/image/add.jpg", UriKind.Relative)),
VerticalAlignment = VerticalAlignment.Center,
Stretch = Stretch.Fill,
Height = 256,
Width = 256
};
}
where Uri("/WpfApplication1;component/image/add.jpg" is address to image and WpfApplication is the name of your WPF Application, image is the folder, add.jpg is the name of image.
The basic approach looks like this:
Button myButton = new Button
{
Width = 50,
Height = 50,
Content = new Image
{
Source = new BitmapImage(new Uri("image source")),
VerticalAlignment = VerticalAlignment.Center
}
};
You need to specify the UriKind on creating BitmapImage
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MyBtn.Content = new Image
{
Source = new BitmapImage(new Uri("/Assets/Imagename.png", UriKind.RelativeOrAbsolute)),
VerticalAlignment = VerticalAlignment.Center,
Stretch = Stretch.Fill,
Height = 256,
Width = 256
};
}
Also please check your image using.
<Image Height="256" Width="256" Source="/Assets/Imagename.png" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With