Drawing 2px Hatch brush in WPF

I am trying to create a WPF DrawingBrush that will draw a hatch pattern using two 1px by 1px rectangles. The resulting template will be similar to the background in desktop Macintosh applications.

Here's what I'm working with:

<Canvas SnapsToDevicePixels="True">
    <Canvas.Background>
        <DrawingBrush x:Name="gridBackgroundBrush" 
        Viewport="0,0,10,10"            
        ViewportUnits="Absolute"
        TileMode="Tile">
          <DrawingBrush.Drawing>
            <DrawingGroup>
              <DrawingGroup.Children>                                   
                <GeometryDrawing Geometry="M0,0 L10,0 10,10, 0,10Z" Brush="Green"/>                 
                <GeometryDrawing Geometry="M10,10 L20,10 20,20, 10,20Z" Brush="Green" />                                
              </DrawingGroup.Children>
            </DrawingGroup>
          </DrawingBrush.Drawing>
        </DrawingBrush>
    </Canvas.Background>
</Canvas>

      

Everything looks clear and sharp, except that the boxes are too big. When I tweak the Viewport on the brushes, everything starts to blur. It looks like anti-aliasing is what is killing me; he wants to use 3px to fade out of solid green into nothing, which doesn't work when I get below 3-4px. Is there anything I can do to turn off anti-aliasing completely and make an accurate drawing?

+1


a source to share


1 answer


Move the picture down from 0.5px and you will get rid of the anti-aliasing effect. This is because the drawing is done at the edge of the pixel offset and not at the actual pixel offset that you specified. By X, Y offset by half a pixel, you tell the paint engine to paint on the pixel itself, which removes the need for anti-aliasing.

For some reason this doesn't work with gradientbrushes.

This behavior is similar to what you have in Quarts drawings on a Mac.



Note. This is not the viewport that you have to offset, but the actual shape that you draw when using the specified brush.

For a more complete answer, read this article .

+1


a source







All Articles