Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用画笔在画布上画棋盘图案?_C#_Wpf_Canvas - Fatal编程技术网

C# 如何用画笔在画布上画棋盘图案?

C# 如何用画笔在画布上画棋盘图案?,c#,wpf,canvas,C#,Wpf,Canvas,我得到了画布和一些矩形,它们的宽度不同,但高度相同。 我在画布上绘制矩形,宽度在绘制时实时计算 一些矩形有SolidColorBrush,但其余的应该看起来像棋盘: 我试着这样做: private static Brush CreateBrush() { // Create a DrawingBrush var blackBrush = new DrawingBrush(); // Create a Geometry with white background

我得到了
画布
和一些
矩形
,它们的
宽度不同
,但高度相同。 我在
画布上绘制
矩形
宽度
在绘制时实时计算

一些
矩形
SolidColorBrush
,但其余的应该看起来像棋盘:

我试着这样做:

private static Brush CreateBrush()
{

    // Create a DrawingBrush
    var blackBrush = new DrawingBrush();
    // Create a Geometry with white background

    // Create a GeometryGroup that will be added to Geometry
    var gGroup = new GeometryGroup();
    gGroup.Children.Add(new RectangleGeometry(new Rect(0, 0, 10, 10)));
    gGroup.Children.Add(new RectangleGeometry(new Rect(10, 10, 10, 10)));
    // Create a GeomertyDrawing
    var checkers = 
        new GeometryDrawing(new SolidColorBrush(Colors.Black), null, gGroup);

    var checkersDrawingGroup = new DrawingGroup();

    checkersDrawingGroup.Children.Add(checkers);

    blackBrush.Drawing = checkersDrawingGroup;

    // Set Viewport and TileMode
    blackBrush.Viewport = new Rect(0, 0, 0.5, 0.5 );
    blackBrush.TileMode = TileMode.Tile;

    return blackBrush; 
}
由于
矩形
的大小不同,相同的画笔在不同的
矩形上看起来不同
——它不会重复填充,而是拉伸

据我所知,问题出在
blackBrush.Viewport=newrect(0,0,0.5,0.5)-我应该根据每个
矩形
宽度
设置不同的
视口
,但我不能接受。我需要为所有不同大小的矩形使用一个画笔,并且纹理应该看起来像一张图片-X和Y的比例相同,但它可以在一个
矩形中重复多次

也许有另一种刷子类型或其他方法来解决这个问题

填免费问,如果我的帖子不清楚,也对不起我的英语

添加我需要的图片:
(这与宽度无关,它与纹理填充有关)

问题在于
视口
设置中,您应该将其
视口单元
设置为
绝对
,而不是
相对边界框
(默认情况下):

下面是一个使用纯XAML显示检查板的示例:

<Grid>
    <Grid.Background>
        <DrawingBrush TileMode="Tile" Viewport="0,0,60,60" 
                                      ViewportUnits="Absolute">
            <DrawingBrush.Drawing>
                <GeometryDrawing Brush="Black" 
                    Geometry="M5,5 L0,5 0,10 5,10 5,5 10,5 10,0 5,0 Z"/>
            </DrawingBrush.Drawing>
        </DrawingBrush>            
    </Grid.Background>
</Grid>

通过使用带有国王答案的绘图组,您可以为每一面指定不同的颜色:

<DrawingBrush.Drawing>
    <DrawingGroup>
        <GeometryDrawing Brush="Black" Geometry="M5,5 L0,5 0,10 5,10 5,5 10,5 10,0 5,0 Z"/>
        <GeometryDrawing Brush="Blue" Geometry="M0,0 L0,5 0,10 0,5, 10,5 10,10 5,10 5,0 Z"/>
    </DrawingGroup>
</DrawingBrush.Drawing>


rest应该看起来像chessfield。。。现在怎么样?请使用合理的描述。@Sheridan我添加图片来解释,抱歉不知道更好的词。啊。。。棋盘。我已相应地编辑了你的问题。然而,我还是不明白你的问题。。。似乎你已经成功地创建了你的棋盘模式。你到底有什么问题?@Sheridan,ty我添加图片来说明我的矩形现在看起来如何,以及我希望它看起来如何。我想要没有变形和拉伸的相同纹理,但只是重复相同的图案以覆盖所有矩形。不幸的是,
DrawingBrush
没有
LinearGradientBrush
所具有的有用的
SpreadMethod
属性,因此我们无法以相同的方式重复它。然而,您面临的真正问题是,
DrawingBrush
的图案将随着使用它的形状的大小而拉伸。。。就像你自己发现的那样。但一定有办法做到这一点。。。继续找。
<DrawingBrush.Drawing>
    <DrawingGroup>
        <GeometryDrawing Brush="Black" Geometry="M5,5 L0,5 0,10 5,10 5,5 10,5 10,0 5,0 Z"/>
        <GeometryDrawing Brush="Blue" Geometry="M0,0 L0,5 0,10 0,5, 10,5 10,10 5,10 5,0 Z"/>
    </DrawingGroup>
</DrawingBrush.Drawing>