【问题标题】:Convert the grid background to image through code behind通过后面的代码将网格背景转换为图像
【发布时间】:2014-04-02 15:55:50
【问题描述】:

我想通过后面的代码把网格背景转成图片,怎么做-

我试过这个-

ImageBrush gridBackground = (ImageBrush)(((Grid)sender).Background);
System.Windows.Controls.Image gridBackImage = new System.Windows.Controls.Image();
gridBackImage.Source = gridBackground.ImageSource;

报错-

无法将“System.Windows.Media.SolidColorBrush”类型的对象转换为“System.Windows.Media.ImageBrush”类型。

【问题讨论】:

  • 你想用图片的gridBackImage做什么?为什么不使用图像刷?
  • 请告诉我如何处理我用代码发布的上述异常?

标签: c# wpf xaml


【解决方案1】:

我觉得您需要进一步澄清您的要求,因为您可能尝试的方法可能是错误的。我觉得您没有将任何图像设置为网格的背景,而只有一种颜色。

因此,将背景设置为一种颜色,它将返回一个 SolidColorBrush 而不是一个图像,即 ImageBrush。

如果您已将其设置为图像背景,那么您的代码将可以正常工作。但问题是你打算用gridBackImage 做什么?因为我觉得我们正在将其转换为不必要的东西。如果你说你打算做的事情会更好地解决。

var grid = sender as Grid;
Image gridBackImage =new Image();
gridBackImage.Source = grid.Background.ImageSource;

【讨论】:

  • rambo 请检查我发布的答案是否正确?现在它没有抛出那个异常
  • 我了解@Saritha.S.R 的回答将不会出错。但我仍然不清楚你想达到什么目标?如果你想用 Image 设置 Grid 或者从已经设置好的网格中获取 Image?
【解决方案2】:
ImageBrush myBrush = new ImageBrush();
  Image image = new Image();
  image.Source = new BitmapImage(
      new Uri(
         "pack://application:,,,/MyClassLibrary;/Images/Image1.jpg"));
  myBrush.ImageSource = image.Source;
  Grid grid = (Grid)sender;
  grid.Background = myBrush;   

【讨论】:

  • 我想将网格背景转换为图像,反之亦然
【解决方案3】:

通过在网格中添加以下代码,您可以轻松地在 xaml 中实现

<Grid>
    <Grid.Background>  
        <ImageBrush ImageSource="/MyProject;component/Images/bg.png"/>     
    </Grid.Background>
</Grid>

留给您做的是向解决方案添加一个名为“Images”的文件夹,并将现有文件添加到新的“Images”文件夹中,在本例中称为“bg.png”

【讨论】:

    【解决方案4】:

    您可以将背景转换为 ImageBrush 而不是 Image。

    ImageBrush img = (Grid_Image.Background as ImageBrush);
    

    【讨论】:

      【解决方案5】:

      您应该能够扭转 Sarita.S.R 的回答,例如:

      ImageBrush myBrush = (ImageBrush)(((Grid)sender).Background);
      Image image = new Image();
      image.Source = myBrush.ImageSource;
      

      这就是你所追求的吗?

      编辑

      您的编辑:

      Unable to cast object of type 'System.Windows.Media.SolidColorBrush' to type System.Windows.Media.ImageBrush'.
      

      表示您实际上没有将图像设置为背景。你需要这样的东西:

      <Grid.Background>
          <ImageBrush ImageSource="YourBackgroundImage.jpg"/>
      </Grid.Background>
      

      或者在后面的代码中:

      ImageBrush myBrush = new ImageBrush();
      Image image = new Image();
      image.Source = new BitmapImage(
          new Uri("pack://application:,,,/YourProject;component/YourBackgroundImage.jpg"));
      myBrush.ImageSource = image.Source;
      yourGrid.Background = myBrush;
      

      为此工作。您根本无法从 SolidColorBrush 中获取图像

      【讨论】:

      • 请检查我已经编辑了我的问题,包含您发布的相同代码,但现在的错误是 - 无法将“System.Windows.Media.SolidColorBrush”类型的对象转换为“System. Windows.Media.ImageBrush'。
      • 请告诉我应该在我作为问题发布的上述代码中操作什么,我必须用后面的代码做所有事情,而不是 xaml
      【解决方案6】:

      请检查图片背景是否设置为ImageBrush,如

         <Grid Height="300" Width="100" x:Name="Grid_Image">
              <Grid.Background>
                  <ImageBrush ImageSource="ms-appx:///Assets/SmallLogo.scale-100.png"/>
              </Grid.Background>
          </Grid>
      

      然后你可以按照你提到的那样输入代码。

          ImageBrush im = ((ImageBrush)(((Grid)sender).Background); 
          Image gridBackImage =new Image();
          gridBackImage.Source = im.ImageSource;
      

      【讨论】:

        【解决方案7】:

        已解决,我的代码是- ImageBrush 网格背景;

                if (((Grid)sender).Children.Count > 0)
                {
                    gridBackground = (ImageBrush)(((Grid)sender).Background);
                    System.Windows.Controls.Image gridBackImage = new System.Windows.Controls.Image();
                    gridBackImage.Source = gridBackground.ImageSource;
        
                    ImageCar.Source = gridBackImage.Source;
                }
        

        【讨论】:

          猜你喜欢
          • 2013-04-10
          • 2019-04-26
          • 1970-01-01
          • 1970-01-01
          • 2019-03-19
          • 1970-01-01
          • 1970-01-01
          • 2017-12-01
          • 2017-07-25
          相关资源
          最近更新 更多