如何将.PNG图像设置为我的WPF表单的TILED背景图像?

Ser*_*pia 16 c# wpf background image

我自己学习WPF,似乎无法找到一种方法来完成这项工作.

这是我的代码:

<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" >
<DockPanel>
    <Menu DockPanel.Dock="Right"
          Height="30"              
          VerticalAlignment="Top"
          Background="#2E404B"
          BorderThickness="2.6">
        <Menu.BitmapEffect>
            <DropShadowBitmapEffect Direction="270" ShadowDepth="3" Color="#2B3841"/>
        </Menu.BitmapEffect>                          
    </Menu>
</DockPanel>
Run Code Online (Sandbox Code Playgroud)

如何显示平铺的背景图像?

Nit*_*esh 51

将ViewportUnits设置为absolute,这将允许您在视口中定义图像的像素大小.在我的示例中,图像大小为32x32.

<Window.Background>
    <ImageBrush ImageSource="image.png" TileMode="Tile" ViewportUnits="Absolute" Viewport="0,0,32,32"/>
</Window.Background>
Run Code Online (Sandbox Code Playgroud)

  • 这是一个更好的答案 (4认同)
  • 哦,老兄,我正在拔头发,试图弄清楚为什么这对我不起作用。原来我在用`ViewboxUnits`而不是`ViewportUnits`:S (2认同)

jfr*_*rej 26

或者,也许你可以使用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>
Run Code Online (Sandbox Code Playgroud)

  • 视口属性设置基础图块的位置和尺寸.看看这里的示例:http://msdn.microsoft.com/en-us/library/system.windows.media.tilebrush.viewport.aspx基本上,"0,0,0.5,0.5"表示基本磁贴将占用从点(0,0)到(0.5,0.5)的空间 - 即从输出区域的左上角到中心.(1,1)是右下角.您应该使用MSDN Library.这真的很有用.所有答案都在那里. (5认同)