alxppp
alxppp

Reputation: 5

WPF: tile image

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

Upvotes: 0

Views: 3349

Answers (2)

anon271334
anon271334

Reputation:

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

Upvotes: 1

Mizipzor
Mizipzor

Reputation: 52321

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.

Upvotes: 1

Related Questions