views:

135

answers:

2

I need to tile a picture in a WPF Image Control (like on Windows Desktop (Background tile property)). Does somebody know weather this is possible and if so, how?

Best regards, Alexander

+1  A: 

If you're drawing your image with a DrawingBrush you can set the property TileMode to TileMode.Tile. Assuming I've understood your question correctly, it does what you want.

mizipzor
A: 

Here is a sample Rectangle that I borrowed from MSDN.

DrawingBrush myBrush = new DrawingBrush();

GeometryDrawing backgroundSquare =
    new GeometryDrawing(
        Brushes.White,
        null,
        new RectangleGeometry(new Rect(0, 0, 100, 100)));

GeometryGroup aGeometryGroup = new GeometryGroup();
aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(0, 0, 50, 50)));
aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(50, 50, 50, 50)));

LinearGradientBrush checkerBrush = new LinearGradientBrush();
checkerBrush.GradientStops.Add(new GradientStop(Colors.Black, 0.0));
checkerBrush.GradientStops.Add(new GradientStop(Colors.Gray, 1.0));

GeometryDrawing checkers = new GeometryDrawing(checkerBrush, null, aGeometryGroup);

DrawingGroup checkersDrawingGroup = new DrawingGroup();
checkersDrawingGroup.Children.Add(backgroundSquare);
checkersDrawingGroup.Children.Add(checkers);

myBrush.Drawing = checkersDrawingGroup;
myBrush.Viewport = new Rect(0, 0, 0.25, 0.25);
myBrush.TileMode = TileMode.Tile;

exampleRectangle.Fill = myBrush;

It demonstrates how to Tile a rectangle. Here is the link to MSDN: WPF Brush Overview - MSDN

lucifer
Thank you for your replies. Now I got the following XAML code:<Rectangle> <Rectangle.Fill> <ImageBrush Viewport="0, 0, 0.25, 0.25" TileMode="Tile" ImageSource="Background1.png"/> </Rectangle.Fill></Rectangle>This tiles the picture; but when I resize the Rectangle the "tiled-pictures" get streched. However, I want them to repeat (like a background image on a HTML webpage).
alxppp
Found the solution:<ImageBrush TileMode="Tile" Viewbox="0, 0, 320, 320" ViewboxUnits="Absolute" Viewport="0, 0, 320, 320" ViewportUnits="Absolute" ImageSource="Background1.png"/>
alxppp
Sorry I couldn't reply to your comment, I just woke up :P I'm glad you got it all sorted. :D
lucifer