How to set a .PNG image as a TILED background image for my WPF Form?

后端 未结 2 949
星月不相逢
星月不相逢 2020-12-15 20:36

I\'m learning WPF on my own and I can\'t seem to find a way to make this work.

Here\'s my code:



        
相关标签:
2条回答
  • 2020-12-15 20:54

    Or, perhaps, you could use Visual Brush:

    <Window
        x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Test" Height="600" Width="800">
      <Window.Background>
        <VisualBrush TileMode="Tile" Viewport="0,0,0.5,0.5">
          <VisualBrush.Visual>
            <Image Source="image.png"></Image>
          </VisualBrush.Visual>
        </VisualBrush>
      </Window.Background>
    </Window>
    
    0 讨论(0)
  • 2020-12-15 21:03

    Set the ViewportUnits to absolute, which will allow you to define the pixel size of your image in the Viewport. In my example the image size is 32x32.

    <Window.Background>
        <ImageBrush ImageSource="image.png" TileMode="Tile" ViewportUnits="Absolute" Viewport="0,0,32,32"/>
    </Window.Background>
    
    0 讨论(0)
提交回复
热议问题