如何在DrawingImage/DrawingContext中绘制清晰的线条?

4
请看图片。我希望中间的线是一条清晰的1像素线。您可以将示例标记复制并粘贴到kaxaml中。

alt text http://img832.imageshack.us/img832/1704/lines.png


<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Image SnapsToDevicePixels="True" Stretch="None">
      <Image.Source>
         <DrawingImage>
            <DrawingImage.Drawing>
               <DrawingGroup>
                  <GeometryDrawing>
                     <GeometryDrawing.Pen>
                        <Pen Brush="Red" Thickness="1"/>
                     </GeometryDrawing.Pen>
                     <GeometryDrawing.Geometry>
                        <LineGeometry StartPoint="0,0" EndPoint="50,0"/>
                     </GeometryDrawing.Geometry>
                  </GeometryDrawing>
                  <GeometryDrawing>
                     <GeometryDrawing.Pen>
                        <Pen Brush="Black" Thickness="1"/>
                     </GeometryDrawing.Pen>
                     <GeometryDrawing.Geometry>
                        <LineGeometry StartPoint="0,5.860" EndPoint="50,5.860"/>
                     </GeometryDrawing.Geometry>
                  </GeometryDrawing>
                  <GeometryDrawing>
                     <GeometryDrawing.Pen>
                        <Pen Brush="Black" Thickness="1"/>
                     </GeometryDrawing.Pen>
                     <GeometryDrawing.Geometry>
                        <LineGeometry StartPoint="0,12" EndPoint="50,12"/>
                     </GeometryDrawing.Geometry>
                  </GeometryDrawing>
               </DrawingGroup>
            </DrawingImage.Drawing>
         </DrawingImage>
      </Image.Source>
   </Image>
</Page>

3个回答

9

完美地工作了,证明了我使用带有DrawingImage的图像作为源而不是通常用于XAML图标的DrawingBrush技术的正确性。 - jpierson

1

将中间的行改为:

<LineGeometry StartPoint="0,6" EndPoint="50,6"/>

通过将线条放置在整数单位上,您可以在像素边界上获得清晰的线条。


我正在寻找一个通用的解决方案,比如一个标志。我在CoreGraphics中遇到了类似的问题,解决方法是将我的上下文转换为0.5,0.5。 - phi

0

我在 Border 控件上遇到了类似的问题,尽管 @phi 的解决方案在大多数情况下都有效,但我还必须添加以下内容才能使其完全工作:

SnapsToDevicePixels="True"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接