以编程方式在按钮上设置图像

3

我在编程中遇到了设置按钮图片的问题。我有一个WPF按钮:

<Button Name="MyBtn" x:FieldModifier="public" HorizontalAlignment="Center" VerticalAlignment="Center" Click="FlashOnOf_Click" Height="256" Width="256" BorderBrush="{x:Null}"/>

尝试在其中设置图像,并使用以下情况:
        var brush = new ImageBrush();
        brush.ImageSource = new BitmapImage(new Uri("ms-appx://Assets/light_bulb_off.png"));
        brush.Stretch = Stretch.Fill;

        MyBtn.Background = brush;

一种更多的情况:
        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
        };

同样没有运气。我在哪里犯了错误?
4个回答

3
你应该像这样设置图片:

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
    };
}

其中Uri("/WpfApplication1;component/image/add.jpg"是图片的地址,WpfApplication是您的WPF应用程序的名称,image是文件夹,add.jpg是图片的名称。

基本方法如下:

Button myButton = new Button
{
    Width = 50,
    Height = 50,
    Content = new Image
    {
        Source = new BitmapImage(new Uri("image source")),
        VerticalAlignment = VerticalAlignment.Center
    }
};

1

在创建BitmapImage时,您需要指定UriKind

    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
        };
    }

同时请检查您的图像。

<Image Height="256" Width="256" Source="/Assets/Imagename.png" />

很遗憾,这并没有帮助。 - Ted
你改过图片的名字了吗?只需复制粘贴代码并重命名图片即可。 - asitis
是的,唯一的区别是我在“Assets”之前使用了“ms-appx://”,否则我会遇到“System.ArgumentException”异常。 - Ted
这段代码在我的电脑上可以运行。尝试给你的按钮添加背景颜色并检查图片的“Build Action”属性。 - asitis

0

尝试使用以下代码将图像设置为按钮背景:

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;
          }

要将图像设置为按钮内容,请尝试以下方法。

        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
            };
          }

-1
问题出在URI名称上: 下一行
Source = new BitmapImage(new Uri("ms-appx://Assets/light_bulb_off.png")),

应该改为:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接