Elan
Elan

Reputation: 6376

How make border stroke of WPF Polygon solid?

If you look at the below XAML, it creates two rectangles.

XAML

<Grid>
    <Rectangle Height="80" Width="300" Fill="Maroon"
        HorizontalAlignment="Center" VerticalAlignment="Bottom">
    </Rectangle>
    <Rectangle Height="300" Width="50" Fill="LightSteelBlue"
        HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="50,0">
    </Rectangle>
    <Polygon Fill="LightSteelBlue" Stroke="LightSteelBlue"
        HorizontalAlignment="Center" VerticalAlignment="Bottom">
        <Polygon.Points>
            <Point X="0" Y="300"/>
            <Point X="50" Y="300"/>
            <Point X="50" Y="0"/>
            <Point X="0" Y="0"/>
            <Point X="0" Y="300"/>
        </Polygon.Points>
    </Polygon>
</Grid>

The polygon is drawn with a border that is not solid, i.e. when you magnify the image you will see the anti-aliased edges. Interestingly when you draw a rectangle, you do not get these (rectangle on left, polygon on right):

Image http://www.barramsoft.com/pub/images/BarBorders2.png

Is there a way to draw the polygon with solid/clean edges?

Upvotes: 0

Views: 3705

Answers (3)

Kah Heng
Kah Heng

Reputation: 41

This is an old question but if anybody happen to chance upon this from Google, here's the solution that worked for me:

Add RenderOptions.EdgeMode="Aliased" to your polygon:

<Polygon ...
         RenderOptions.EdgeMode="Aliased">
    <Polygon.Points>
        ...
    </Polygon.Points>
</Polygon>

Here's MSDN Documentation for it.

Upvotes: 4

Emond
Emond

Reputation: 50682

The translucency you see is not caused by a non-solid border or a border that is not thick enough but by anti-aliasing.

Setting SnapsToDevicePixels="True" will not solve this as a rectangle is a Drawing object so you will have to use Guidelines

Another way is to 'fix' it is by putting the lines in the middle of the pixels:

     <Polygon.Points>
            <Point X="0.5"
                   Y="300.5" />
            <Point X="50.5"
                   Y="300.5" />
            <Point X="50.5"
                   Y="0.5" />
            <Point X="0.5"
                   Y="0.5" />
            <Point X="0.5"
                   Y="300.5" />
        </Polygon.Points>

When the coordinates are given like this it is easier to decide what pixels to turn on. If the coordinate is in between two (or more) pixels WPF will color all of them a bit.

Upvotes: 1

Mark Synowiec
Mark Synowiec

Reputation: 5445

Set a thickness:

StrokeThickness="5"

Also you might need to snap to device pixels:

SnapsToDevicePixels="True"

Upvotes: 1

Related Questions