如何改变在C#WPF code \\设置按钮的背景图像? [英] How to change\set button background image in C# WPF code?

查看:222
本文介绍了如何改变在C#WPF code \\设置按钮的背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变我的按钮图像背景其他一些形象,我遇到了一些错误。
这是code我有我的XAML:

Hi i'm trying to change my button image background to some other image and i encountered some errors. this is the code i have on my xaml:

    <Button x:Name="Button1" Width="200" Height="200" Content="Button1" Margin="0,0,0,400">
        <Button.Background>
            <ImageBrush **ImageSource ="Images/AERO.png"**  ></ImageBrush>
        </Button.Background>
    </Button>

和我的CS:

    private void Button1_Click_1(object sender, RoutedEventArgs e)
    {
        var brush = new ImageBrush();
        brush.ImageSource = new BitmapImage(new Uri("Images/AERO.png"));
        Button1.Background = brush;
    }

错误我有我的XAML是文件形象\\ logo.png没有项目或它的生成操作属性设置为资源的一部分。
谁能帮我解释一下,谢谢

the error i have on my xaml is " The file 'Images\logo.png' is not part of the project or its 'Build Action' property is not set to 'Resource'. Can anyone help me explain, thanks

推荐答案

在生成操作,你可以标记的图像文件的内容或资源。使用图像中的ImageBrush的语法取决于你选择哪一个是不同的。

In the build action, you can mark the image file as content or as resource. The syntax to use the image in an ImageBrush is different depending on which one you choose.

下面是标记为一个图像文件的内容

Here is a image file marked as content.

要设置按钮的背景到该图像使用下面的code。

To set the button background to this image use the following code.

 var brush = new ImageBrush();
 brush.ImageSource = new BitmapImage(new Uri("Images/ContentImage.png",UriKind.Relative));
 button1.Background = brush;

下面是标记为一个图像文件的资源

Here is an image file marked as resource.

要按钮背景设置为资源图片请使用以下code。

To set the button background to the resource image use the following code.

  Uri resourceUri = new Uri("Images/ResourceImage.png", UriKind.Relative);
  StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

  BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
  var brush = new ImageBrush();
  brush.ImageSource = temp;

  button1.Background = brush;

这篇关于如何改变在C#WPF code \\设置按钮的背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆