在WPF中转换时文本模糊

时间:2014-04-09 05:51:06

标签: c# wpf xaml

我正在使用DrawingContext.DrawTextDrawingContext.PushTransfrom在WPF中的可视图层上创建旋转文本,但正如您在下图中看到的那样,旋转的文本在图像的某些区域相当模糊。

我可以使用任何选项来改善这一点吗? Arial 字体用于文字。

enter image description here

public class BeamTextDrawing : FrameworkElement
{
    private readonly VisualCollection _visuals;
    public BeamTextDrawing(double scale)
    {
        if (scale <= 0)
        {
            scale = 1;
        }
        var typeface = Settings.BeamTextTypeface;
        var cultureinfo = Settings.CultureInfo;
        var flowdirection = Settings.FlowDirection;
        var textsize = Settings.BeamTextSize / scale;
        var beamtextcolor = Settings.InPlanBeamTextColor;

        _visuals = new VisualCollection(this);

        foreach (var beam in Building.BeamsInTheElevation)
        {
            var drawingVisual = new DrawingVisual();
            using (var dc = drawingVisual.RenderOpen())
            {
                var text = Convert.ToString(beam.Section.Id);
                //text = scale.ToString();
                var ft = new FormattedText(text, cultureinfo, flowdirection,
                                            typeface, textsize, beamtextcolor)
                {
                    TextAlignment = TextAlignment.Center
                };

                var x1 = beam.ConnectivityLine.I.X;
                var y1 = beam.ConnectivityLine.I.Y;
                var x2 = beam.ConnectivityLine.J.X;
                var y2 = beam.ConnectivityLine.J.Y;

                var v1 = new Point(x2, y2) - new Point(x1, y1);
                var v2 = new Vector(1, 0);

                var hwidth = textsize;
                var l = Geometrics.GetOffset(x1, y1, x2, y2, hwidth + 5/scale);

                var angle = Vector.AngleBetween(v1, v2);
                var x = 0.5 * (l.X1 + l.X2);
                var y = 0.5 * (l.Y1 + l.Y2);

                var r = new RotateTransform(angle, x, SelectableModel.FlipYAxis(y));
                dc.PushTransform(r);
                dc.DrawText(ft, SelectableModel.FlipYAxis(x, y));
            }
            _visuals.Add(drawingVisual);
        }
    }

    protected override Visual GetVisualChild(int index)
    {
        return _visuals[index];
    }

    protected override int VisualChildrenCount
    {
        get
        {
            return _visuals.Count;
        }
    }
}

更新

以下是使用此代码后的图像:

TextOptions.SetTextFormattingMode(this, TextFormattingMode.Display);

我的结果仍然模糊。查看图像下部的中间光束文本。

enter image description here

1 个答案:

答案 0 :(得分:0)

查看此文章 - Pixel Snapping in WPF Applications

由于WPF使用与设备无关的像素,因此在进行转换时由于抗锯齿而会产生不规则的边缘渲染。像素贴图是一种通过将小偏移应用于视觉几何体以将几何图形与设备像素对齐来抑制这些视觉伪影的方法。

SnapsToDevicePixels属性设置为&#34; True&#34;对于您的UI元素应该能够解决您的问题。