【问题标题】:Draw an antialias ellipse region for label为标签绘制抗锯齿椭圆区域
【发布时间】:2017-12-31 01:27:29
【问题描述】:

我为标签绘制了一个类似椭圆的区域,但我不知道如何为其设置抗锯齿。

片段:

Rectangle circle = new Rectangle(0, 0, labelVoto.Width,labelVoto.Height);
var path = new GraphicsPath();
path.AddEllipse(circle);
labelVoto.Region = new Region(path);

结果如下:

谁能帮帮我?

【问题讨论】:

  • 区域不支持抗锯齿。请记住,Label 是一种非常昂贵的方式来避免将代码添加到其父级的 Paint 事件中。

标签: c# winforms label drawing


【解决方案1】:

设置Graphics 对象的SmoothingMode。覆盖OnPaintBackground 而不是更改Region。区域不支持抗锯齿。此示例通过从 Label 派生来创建自定义标签。

public class EllipticLabel : Label
{
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // This ensures that the corners of the label will have the same color as the
        // container control or form. They would be black otherwise.
        e.Graphics.Clear(Parent.BackColor);

        // This does the trick
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

        var rect = ClientRectangle;
        rect.Width--;
        rect.Height--;
        using (var brush = new SolidBrush(BackColor)) {
            e.Graphics.FillEllipse(brush, rect);
        }
    }
}

如果您将绘制矩形大小设置为等于ClientRectangle。椭圆将在右侧和底部被剪裁一个像素。因此,我将其大小减小了一个像素。

您可以通过在代码或属性窗口中设置标签的BackColor 属性来设置所需的椭圆背景颜色。

结果:

编译代码后,自定义标签会自动出现在当前项目的Toolbox中。

【讨论】:

  • 但他询问了地区
猜你喜欢
  • 2010-10-03
  • 1970-01-01
  • 2010-12-19
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多