English 中文(简体)
How to set a .PNG image as a TILED background image for my WPF Form?
原标题:

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

Here s my code:

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

How can I make a tiled background image appear?

最佳回答

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>

The Viewport property sets the position and dimensions of the base tile. Have a look at examples here.

Basically, "0,0,0.5,0.5" means that the base tile will take space from point (0,0) to (0.5,0.5) - i.e. from the upper left corner of the output area to centre. (1,1) is the lower right corner. You should make use of MSDN Library. It s really useful. All the answers are there.

问题回答

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>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签