programing

C#을 사용하여 WPF의 그리드에 배경 이미지 설정

stoneblock 2023. 4. 14. 21:04

C#을 사용하여 WPF의 그리드에 배경 이미지 설정

문제가 있습니다.뒤에 있는 코드를 통해 그리드 이미지를 설정하고 싶습니다.

이거 어떻게 하는지 누가 좀 알려줄래?

이 모든 것을 그리드에 다음 코드를 추가하면 xaml에서 쉽게 확인할 수 있습니다.

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

이제 'Images'라는 솔루션에 폴더를 추가하고 기존 파일을 새 'Images' 폴더(이 경우 'bg.png')에 추가하는 작업을 수행합니다.

Background Property를 잊으셨습니까?브러시는 ImageSource를 이미지 경로로 설정할 수 있는 ImageBrush여야 합니다.

<Grid>
    <Grid.Background>
        <ImageBrush ImageSource="/path/to/image.png" Stretch="UniformToFill"/>
    </Grid.Background>

    <...>
</Grid>

이미지는 별도의 클래스 라이브러리('MyClassLibrary')에 있으며, 이미지 폴더에 저장됩니다.이 예에서는 배경 이미지로 "myImage.jpg"를 사용했습니다.

  ImageBrush myBrush = new ImageBrush();
  Image image = new Image();
  image.Source = new BitmapImage(
      new Uri(
         "pack://application:,,,/MyClassLibrary;component/Images/myImage.jpg"));
  myBrush.ImageSource = image.Source;
  Grid grid = new Grid();
  grid.Background = myBrush;          

경로 문제를 방지하려면 이 작업을 수행해 보십시오. 배경 이미지를 이미지 폴더에 보관하고 이 코드를 추가하십시오.

<Grid>
  <Grid.Background>
    <ImageBrush Stretch="Fill" ImageSource="..\Images\background.jpg"
                AlignmentY="Top" AlignmentX="Center"/>
  </Grid.Background>
</Grid>

언급URL : https://stackoverflow.com/questions/3100837/set-background-image-on-grid-in-wpf-using-c-sharp