我有一个SVG图形,在400x400视图框的中心是一个264x264的正方形,并且正方形周围的68像素填充是图像的重要部分,我希望它出现在WPF中。我正在使用一个库将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>
生成的图像是一个完全填充其容器的正方形,而不是像我想要的那样在它周围留下一些空的空间。如何让WPF保留该空间而不是将其裁剪掉?
答案 0 :(得分:0)
使用DrawingImage
时,渲染Image
时会忽略/裁剪空格。这篇文章讨论了同样的问题:Why is a GeometryDrawing displayed on Canvas with clipped coordinates?
您可以通过将空白空间绘制为绿色矩形后面的透明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>
...