簡體   English   中英

通過后面的代碼將網格背景轉換為圖像

[英]Convert the grid background to image through code behind

我想通過后面的代碼將網格背景轉換為圖像,如何做到這一點-

我已經試過了

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”的對象。

我覺得您需要進一步澄清您的要求,因為您嘗試的方法可能是錯誤的方法。 我覺得您沒有將任何圖像設置為網格背景,而只是將顏色設置為背景。

因此,將背景設置為顏色將返回SolidColorBrush而不是Image,即ImageBrush。

如果您將其設置為圖像背景,則您的代碼可以正常工作。 但是問題是,您打算對gridBackImagegridBackImage 因為我覺得我們正在將其轉換為不必要的東西。 如果您說自己打算做什么,最好解決。

var grid = sender as Grid;
Image gridBackImage =new Image();
gridBackImage.Source = grid.Background.ImageSource;
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;   

通過在網格中添加以下代碼,可以輕松地在xaml中實現

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

剩下要做的就是在解決方案中添加一個名為“ Images”的文件夾,並在新的“ Images”文件夾中添加一個現有文件,在本例中為“ bg.png”

您應該能夠反轉Saritha.SR的答案,例如:

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獲取圖像

您可以將Backround轉換為ImageBrush而不是Image。

ImageBrush img = (Grid_Image.Background as ImageBrush);

請檢查圖像背景是否設置為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;

解決了,我的代碼是-ImageBrush gridBackground;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM