2011-09-06 95 views
1

我試圖在圓圈內畫'P'失敗。 由於性能問題,我正在使用DrawingConntext。使用DrawingContext時的繪製佈局問題

這就是我得到:

enter image description here

主窗口XAML:

<StackPanel Background="LightGray"> 
    <WrapPanel x:Name="panel"> 
     <Image Width="100" Height="100"> 
      <Image.Source> 
       <MultiBinding Converter="{StaticResource @con2}"> 
       </MultiBinding> 
      </Image.Source> 
     </Image> 
    </WrapPanel> 
</StackPanel> 

轉換方法:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     Geometry circlePath = Geometry.Parse("M 12.5,1 A 11,11 0 1 0 13.5,1 Z"); 

     var dGroup = new DrawingGroup(); 

     using (DrawingContext dc = dGroup.Open()) 
     { 
      dc.DrawGeometry(Brushes.LightBlue, new Pen(Brushes.Black, 1.5), circlePath); 

      //Draw letter 'P' 
      var x = 
       Geometry.Parse(
        "F1 M 0,15.4L 0,0L 5.0665,0C 6.98703,0 8.23807,0.0767822 8.81958,0.230591C 9.71523,0.46228 10.4655,0.966919 11.0704,1.74451C 11.6753,2.52209 11.9778,3.52576 11.9778,4.75574C 11.9778,5.70251 11.8001,6.49963 11.4447,7.14685C 11.0894,7.79407 10.6387,8.30212 10.0929,8.67078C 9.54701,9.03955 8.99112,9.28406 8.42522,9.40442C 7.65878,9.55139 6.547,9.625 5.08989,9.625L 2.99445,9.625L 2.99445,15.4L 0,15.4 Z M 2.99445,2.56665L 2.99445,7.05835L 4.8526,7.05835C 6.19164,7.05835 7.08673,6.97534 7.5379,6.80933C 7.98907,6.64343 8.34277,6.3844 8.599,6.03235C 8.85522,5.6803 8.98334,5.27039 8.98334,4.80249C 8.98334,4.22546 8.80342,3.74976 8.4436,3.37537C 8.08377,3.0011 7.62869,2.76721 7.07838,2.67358C 6.67288,2.60229 5.85855,2.56665 4.63538,2.56665L 2.99445,2.56665 Z "); 
      dc.DrawGeometry(Brushes.Black, new Pen(Brushes.Black, 0), x); 
     } 

     return new DrawingImage(dGroup); 
    } 
  • UPDATE:

改變路徑是不是我想進入, 有沒有一種方式設置它與MARGIN/STRECH/TRANSFORM等?

+1

的符號「P」你的路徑必須被改變,以抵消左邊和頂部頁邊距,這樣它會「看」像它的圓內。但是說這看起來像是一種非常簡單的繪製嵌套gemometry的方法......也許'GeometryDrawing'就是你可能要找的東西.... http://msdn.microsoft.com/en-us/library/system .windows.media.geometrydrawing.aspx –

+0

@AngelWPF - 感謝我更新我的任務。 我看不到GeometryDrawing如何幫助我。 – Erez

+0

通過一些TextBlock添加「P」的第二位應該有所幫助...我想知道... http://stackoverflow.com/questions/2190469/wpf-geometry-path-partly-filled-shapes –

回答

2

您可以將「P」相對於圓心居中。

爲此,請將TranslateTransform加到DrawingContext。要獲得正確的X值,請使用Circle/2.0的寬度,然後減去「P」/ 2.0的寬度。這同樣適用於Y.

enter image description here

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
{ 
    Geometry circlePath = Geometry.Parse("M 12.5,1 A 11,11 0 1 0 13.5,1 Z"); 

    var dGroup = new DrawingGroup(); 

    using (DrawingContext dc = dGroup.Open()) 
    { 
     dc.DrawGeometry(Brushes.LightBlue, new Pen(Brushes.Black, 1.5), circlePath); 
     //Draw letter 'P' 
     var x = Geometry.Parse("F1 M 0,15.4L 0,0L 5.0665,0C 6.98703,0 8.23807,0.0767822 8.81958,0.230591C 9.71523,0.46228 10.4655,0.966919 11.0704,1.74451C 11.6753,2.52209 11.9778,3.52576 11.9778,4.75574C 11.9778,5.70251 11.8001,6.49963 11.4447,7.14685C 11.0894,7.79407 10.6387,8.30212 10.0929,8.67078C 9.54701,9.03955 8.99112,9.28406 8.42522,9.40442C 7.65878,9.55139 6.547,9.625 5.08989,9.625L 2.99445,9.625L 2.99445,15.4L 0,15.4 Z M 2.99445,2.56665L 2.99445,7.05835L 4.8526,7.05835C 6.19164,7.05835 7.08673,6.97534 7.5379,6.80933C 7.98907,6.64343 8.34277,6.3844 8.599,6.03235C 8.85522,5.6803 8.98334,5.27039 8.98334,4.80249C 8.98334,4.22546 8.80342,3.74976 8.4436,3.37537C 8.08377,3.0011 7.62869,2.76721 7.07838,2.67358C 6.67288,2.60229 5.85855,2.56665 4.63538,2.56665L 2.99445,2.56665 Z "); 

     double centerX = circlePath.Bounds.Left + circlePath.Bounds.Width/2; 
     double pWidth = x.Bounds.Width/2; 
     double centerY = circlePath.Bounds.Top + circlePath.Bounds.Height/2; 
     double pHeight = x.Bounds.Height/2; 
     TranslateTransform translateTransform = new TranslateTransform(centerX - pWidth, centerY - pHeight); 
     dc.PushTransform(translateTransform); 

     dc.DrawGeometry(Brushes.Black, new Pen(Brushes.Black, 0), x); 
    } 

    return new DrawingImage(dGroup); 
}