简体   繁体   English

如何防止WPF裁剪矢量图形?

[英]How do I prevent WPF from cropping my vector graphics?

I have an SVG graphic, which is a 264x264 square in the center of a 400x400 viewbox, and that 68 pixels of padding around the square is an important part of the image, I want that to show up in WPF. 我有一个SVG图形,它是400x400视图框中心的264x264正方形,并且正方形周围68像素的填充是图像的重要部分,我希望将其显示在WPF中。 I'm using a library to convert the SVG drawing into a WPF DrawingImage, and the output after I've crammed it into a page to preview it looks like this: 我正在使用一个库将SVG绘图转换为WPF DrawingImage,并将其塞入页面以进行预览后的输出如下所示:

<Page x:Class="Page1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d" 
  d:DesignHeight="400" d:DesignWidth="400"
Title="Page1">

<Image>
    <Image.Source>
        <DrawingImage>
            <DrawingImage.Drawing>
                <DrawingGroup xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:svg="http://sharpvectors.codeplex.com/runtime/">
                    <DrawingGroup.Children>
                        <DrawingGroup>
                            <DrawingGroup.Children>
                                <DrawingGroup>
                                    <DrawingGroup.Children>
                                        <DrawingGroup>
                                            <DrawingGroup.Children>
                                                <GeometryDrawing Brush="#FFAAFFAA">
                                                    <GeometryDrawing.Pen>
                                                        <Pen Brush="#FF000000" Thickness="10" StartLineCap="Flat" EndLineCap="Flat" LineJoin="Miter" />
                                                    </GeometryDrawing.Pen>
                                                    <GeometryDrawing.Geometry>
                                                        <RectangleGeometry RadiusX="0" RadiusY="0" Rect="68,68,264,264" />
                                                    </GeometryDrawing.Geometry>
                                                </GeometryDrawing>
                                            </DrawingGroup.Children>
                                            <DrawingGroup.ClipGeometry>
                                                <RectangleGeometry Rect="0,0,400,400" />
                                            </DrawingGroup.ClipGeometry>
                                            <DrawingGroup.Transform>
                                                <TranslateTransform X="0" Y="0" />
                                            </DrawingGroup.Transform>
                                        </DrawingGroup>
                                    </DrawingGroup.Children>
                                </DrawingGroup>
                            </DrawingGroup.Children>
                            <DrawingGroup.ClipGeometry>
                                <RectangleGeometry Rect="0,0,400,400" />
                            </DrawingGroup.ClipGeometry>
                        </DrawingGroup>
                    </DrawingGroup.Children>
                </DrawingGroup>
            </DrawingImage.Drawing>
        </DrawingImage>
    </Image.Source>
</Image>
</Page>

The resulting image is a square that completely fills its container, instead of leaving some empty space around it like I want. 生成的图像是一个完全填充其容器的正方形,而不是像我想要的那样在其周围留出一些空白。 How do I get WPF to preserve that space instead of cropping it out? 我如何获得WPF保留该空间而不是裁剪掉?

When you use a DrawingImage , empty space is ignored/cropped when rendering the Image . 使用DrawingImage时,渲染Image时将忽略/剪切空白区域。 This post discusses the same problem: Why is a GeometryDrawing displayed on Canvas with clipped coordinates? 这篇文章讨论了同样的问题: 为什么在画布上显示带有裁剪坐标的GeometryDrawing?

You can achieve what you want by drawing the empty space as a transparent 400x400 rectangle behind the green rectangle: 您可以通过将空白绘制为绿色矩形后面的透明400x400矩形来实现所需的功能:

...
<DrawingGroup>
    <DrawingGroup.Children>

        <!-- Added transparent "spacer": -->
        <GeometryDrawing Brush="Transparent">
            <GeometryDrawing.Geometry>
                <RectangleGeometry RadiusX="0" RadiusY="0" Rect="0,0,400,400" />
            </GeometryDrawing.Geometry>
        </GeometryDrawing>

        <!-- This was here before: -->
        <GeometryDrawing Brush="#FFAAFFAA">
            <GeometryDrawing.Pen>
                <Pen Brush="#FF000000" Thickness="10" 
                     StartLineCap="Flat" EndLineCap="Flat" LineJoin="Miter" />
            </GeometryDrawing.Pen>
            <GeometryDrawing.Geometry>
                <RectangleGeometry RadiusX="0" RadiusY="0" Rect="68,68,264,264" />
            </GeometryDrawing.Geometry>
        </GeometryDrawing>
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM