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