【问题标题】:DrawEllipse: Antialiasing broken with PenAlignment.InsetDrawEllipse:使用 PenAlignment.Inset 破坏抗锯齿
【发布时间】:2018-11-07 14:00:59
【问题描述】:

作为this previous question 上的suggested by @TaW,我设置PenAlignment.Inset 在位图内绘制圆圈,但这又引发了另一个问题。


我想用抗锯齿在指定的位图上画一个圆。

SmoothingMode.AntiAlias

问题是,当我使用PenAlignment.Inset 时,抗锯齿功能无法正常工作!
相反,使用PenAlignment.Center,它可以正常工作...
有什么解决这个问题的建议吗?

Bitmap layer = new Bitmap(80, 80);
using (Graphics g = Graphics.FromImage(layer))
{
    using (Pen p = new Pen(Color.Black, 4))
    {
        p.Alignment = PenAlignment.Inset;
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.DrawEllipse(p, new Rectangle(0, 0, layer.Width, layer.Height));
    }
}
pictureBox3.Size = new Size(100, 100);
pictureBox3.Image = layer;


(注意左图的错误)

【问题讨论】:

    标签: c# bitmap antialiasing graphic drawellipse


    【解决方案1】:

    将边界矩形缩小 1/2 笔的笔划宽度应该可以解决这个问题。 “放气”是指将所有 4 个边向矩形中心拉 1/2 笔宽:

    float halfPenWidth = p.Width*0.5f;
    g.DrawEllipse(p, new RectangleF(halfPenWidth, halfPenWidth, layer.Width - p.Width, layer.Height - p.Width));
    

    或插入硬编码笔宽 4:

    g.DrawEllipse(p, new Rectangle(2, 2, layer.Width - 4, layer.Height - 4));
    

    请注意,必须从矩形的宽度和高度中减去整个笔宽,以便将右侧和底部拉入 1/2 笔宽,同时保持矩形在同一点上居中。

    使用此代码与笔对齐居中,笔划宽度的 1/2 将在椭圆与矩形接触的点处绘制在矩形之外,但仍将在位图内绘制。

    【讨论】:

    • 这有点太过分了。只需要引入抗锯齿像素; PenAlignment.Inset 已经处理了笔宽。所以 rect.Inflate(-1, -1);应该做..
    • 但我认为问题在于@FirestormXYZ 不能使用 PenAlignment.Inset 因为抗锯齿在这种情况下无法正常工作。因此需要通过将 PenAlignment 设置为 'Center' 并将边界框缩小 1/2 笔宽来模拟“插入”绘图。
    猜你喜欢
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 2011-07-05
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多