WPF:平铺图像

时间:2010-08-24 10:25:41

标签: wpf image background tile

我需要在WPF图像控件中平铺图片(如在Windows桌面上(背景图块属性))。有人知道天气这是可能的,如果有,怎么样?

祝你好运, 亚历山大

2 个答案:

答案 0 :(得分:1)

如果您使用DrawingBrush绘制图片,可以将属性TileMode设置为TileMode.Tile。假设我已经正确地理解了你的问题,它可以做你想要的。

答案 1 :(得分:1)

这是我从MSDN借来的一个Rectangle示例。

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;

它演示了如何平铺矩形。这是MSDN的链接: WPF Brush Overview - MSDN