简体   繁体   中英

Set image programmatically on button

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" />

Try this to set image as button background,

private void Button_Click_1(object sender, RoutedEventArgs e)
          {
            Button button = sender as Button;
            ImageBrush brush = new ImageBrush();
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource=new Uri (@"C:/imagename.jpg",UriKind.Absolute);
            bitmap.EndInit();
            brush.ImageSource = bitmap;
            button.Background = brush;
          }

To set image as button content, try this.

        private void Button_Click_1(object sender, RoutedEventArgs e)
           {
            Button button = sender as Button;
            button.Content = new Image
            {
                Source = new BitmapImage(new Uri("/Images/imagename.JPG", UriKind.RelativeOrAbsolute)),
                VerticalAlignment = VerticalAlignment.Center,
                Stretch = Stretch.Fill,
                Height = 256,
                Width = 256
            };
          }

Issue was in URI name: Next line

Source = new BitmapImage(new Uri("ms-appx://Assets/light_bulb_off.png")),

should be changed to this:

Source = new BitmapImage(new Uri("ms-appx:///Assets/light_bulb_off.png", UriKind.Absolute)),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM