2012-01-30 23 views
0

如果您查看下面的XAML,它會創建兩個矩形。如何讓WPF Polygon的邊框筆畫變爲實體?

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> 

多邊形繪製帶有邊框是不牢固,當你放大的圖像,你會看到抗鋸齒邊緣即。有趣的是,當你畫一個矩形,你沒有得到這些(左,多邊形矩形右):

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

有沒有一種方法來繪製固/整齊的邊緣多邊形?

回答

1

設置厚度:

StrokeThickness = 「5」

你也可能需要捕捉到設備像素:

SnapsToDevicePixels = 「真」

1

你看到的半透明不由非固體邊界或不夠厚的邊界引起,但是通過抗鋸齒引起。

設置SnapsToDevicePixels="True"不會解決這個問題作爲一個矩形是一個圖形對象,所以你將不得不use Guidelines

另一種方式是「修復」它是通過將線在像素的中間:

 <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> 

當給出像這樣的座標時,更容易決定打開哪些像素。如果座標位於兩個(或更多)像素之間,WPF會將它們全部着色一點。

4

這是一個老問題,但如果有人從谷歌已在此發生的機會,這裏是爲我工作的解決方案:

添加RenderOptions.EdgeMode =「別名」到你的多邊形:

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

這是MSDN Documentation