使用DrawingContext时的绘图布局问题

时间:2011-09-06 07:46:23

标签: c# wpf xaml geometry drawingcontext

我尝试在圆圈内绘制'P'失败。 由于性能问题,我正在使用DrawingConntext。

这就是我得到的:

enter image description here

MainWindow 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);
    }
  • 更新:

改变路径不是我想要进入的, 有没有办法用MARGIN / STRECH / TRANSFORM等设置它?

1 个答案:

答案 0 :(得分:2)

您可以将“P”置于圆圈中心。

为此,请将TranslateTransform推送到DrawingContext。要获得正确的X值,请使用圆的宽度/ 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);
}