【问题标题】:Change WPF window background image in C# code在 C# 代码中更改 WPF 窗口背景图像
【发布时间】:2010-10-24 18:43:30
【问题描述】:

我有几个配置为应用程序资源的图像。

当我的应用程序启动时,主窗口的背景是通过 XAML 设置的:

<Window.Background>
    <ImageBrush ImageSource="/myapp;component/Images/icon.png" />
</Window.Background>

如果发生给定事件,我想将此背景更改为另一个资源 ("/myapp;component/Images/icon_gray.png")。

我尝试过使用两个常量:

private static readonly ImageBrush ENABLED_BACKGROUND =
    new ImageBrush(new BitmapImage(new Uri("/myapp;component/Images/icon.png")));
private static readonly ImageBrush DISABLED_BACKGROUND =
    new ImageBrush(new BitmapImage(new Uri("/myapp;component/Images/icon_gray.png")));

...但很自然地,我得到了 Invalid URI 的异常。

有没有一种简单的方法可以使用包 Uri 或资源(即:Myapp.Properties.Resources.icon)来更改 WPF 窗口的背景图像(通过 this.Background = ...)?

【问题讨论】:

    标签: c# wpf background-image


    【解决方案1】:

    这个呢:

    new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Images/icon.png")))
    

    或者,这个:

    this.Background = new ImageBrush(new BitmapImage(new Uri(@"pack://application:,,,/myapp;component/Images/icon.png")));
    

    【讨论】:

    • 使用第二个,因为我定义了一个静态常量。谢谢!
    【解决方案2】:

    这里是 XAML 版本

    <Window.Background>
        <ImageBrush>
            <ImageBrush.ImageSource>
                <BitmapImage UriSource="//your source .."/>
            </ImageBrush.ImageSource>
        </ImageBrush>
    </Window.Background>
    

    【讨论】:

      【解决方案3】:

      问题在于您在代码中使用它的方式。试试下面的代码

      public partial class MainView : Window
      {
          public MainView()
          {
              InitializeComponent();
      
              ImageBrush myBrush = new ImageBrush();
              myBrush.ImageSource =
                  new BitmapImage(new Uri("pack://application:,,,/icon.jpg", UriKind.Absolute));
              this.Background = myBrush;
          }
      }
      

      您可以在
      http://msdn.microsoft.com/en-us/library/aa970069.aspx

      中找到有关此的更多详细信息

      【讨论】:

        【解决方案4】:

        我只在 "d drive-->Data-->IMG" 中放置一张图片。图片名称为x.jpg:

        关于 c# 代码类型

        ImageBrush myBrush = new ImageBrush();
        
        myBrush.ImageSource = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "D:\\Data\\IMG\\x.jpg"));
        

        (请在路径之间加上双斜杠)

        this.Background = myBrush;
        

        我终于知道了背景..

        【讨论】:

          【解决方案5】:
          Uri resourceUri = new Uri(@"/cCleaner;component/Images/cleanerblack.png", UriKind.Relative);
                      StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);
                      BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
                      var brush = new ImageBrush();
                      brush.ImageSource = temp;
                      frame8.Background = brush;
          

          【讨论】:

            【解决方案6】:

            我一直在这里尝试所有答案,但没有成功。这是使用 ms-appx 的最简单方法

                    ImageBrush myBrush = new ImageBrush();
                    Image image = new Image();
                    image.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/background.jpg"));
                    myBrush.ImageSource = image.Source;
                    TheGrid.Background = myBrush;
            

            Assets 文件夹位于我项目的第一级,所以请确保更改路径方便。

            【讨论】:

              【解决方案7】:

              img.UriSource = new Uri("pack://application:,,,/images/" + fileName, UriKind.Absolute);

              【讨论】:

                猜你喜欢
                • 2013-03-31
                • 2010-12-12
                • 1970-01-01
                • 2015-08-30
                • 1970-01-01
                • 2015-08-20
                • 2016-07-12
                • 1970-01-01
                • 2010-11-21
                相关资源
                最近更新 更多