1

我有代码:

<Canvas>
    <Image Canvas.Left="0" Canvas.Top="0">
        <Image.Source>
            <DrawingImage>
                <DrawingImage.Drawing>
                    <GeometryDrawing>
                        <GeometryDrawing.Pen>
                            <Pen Brush="Black" Thickness="1" />
                        </GeometryDrawing.Pen>
                        <GeometryDrawing.Geometry>
                            <LineGeometry StartPoint="50,50" EndPoint="100,50">
                            </LineGeometry>
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                </DrawingImage.Drawing>
            </DrawingImage>
        </Image.Source>
    </Image>
</Canvas>

我希望它可以是从 50,50 到 100,50 的一条线,但最后它从 0,0 到 50,0 我似乎StartPoint没有GeometryDrawing意义?有谁知道解决方案?我不想修改Canvas.Leftand Canvas.Top

4

2 回答 2

2

显然 aDrawingImage已调整为实际绘制的几何图形的边界。为了解决这个问题,您可以将 a 替换为LineGeometry包含PathGeometrypoint的 a (0,0),但不绘制它:

<GeometryDrawing.Geometry>
    <PathGeometry>
        <PathFigure StartPoint="0,0">
            <LineSegment Point="50,50" IsStroked="False"/>
            <LineSegment Point="100,50"/>
        </PathFigure>
    </PathGeometry>
</GeometryDrawing.Geometry>

请注意,这StartPoint="0,0"是默认值。只是为了清楚起见。

于 2013-07-18T10:34:07.720 回答
0

通过在所有内容周围使用透明画笔绘制矩形几何图形,我取得了很好的效果。您可以在DrawingGroup.

<Canvas>
    <Image Canvas.Left="0" Canvas.Top="0">
        <Image.Source>
            <DrawingImage>
                <DrawingImage.Drawing>

                    <DrawingGroup>

                    <GeometryDrawing Brush="Transparent"/>
                         <RectangleGeometry>
                             <!-- here you create a rectangle with desired bounds -->
                         </RectangleGeometry>
                    </GeometryDrawing>

                    <GeometryDrawing>
                        <GeometryDrawing.Pen>
                            <Pen Brush="Black" Thickness="1" />
                        </GeometryDrawing.Pen>
                        <GeometryDrawing.Geometry>
                            <LineGeometry StartPoint="50,50" EndPoint="100,50">
                            </LineGeometry>
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>

                    </DrawingGroup>

                </DrawingImage.Drawing>
            </DrawingImage>
        </Image.Source>
    </Image>
</Canvas>
于 2014-01-16T16:42:57.177 回答