DrawEllipse:用PenAlignment.Inset破坏抗锯齿 [英] DrawEllipse: Antialiasing broken with PenAlignment.Inset

查看:223
本文介绍了DrawEllipse:用PenAlignment.Inset破坏抗锯齿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

用笔的笔触宽度的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绘制到矩形外部,但仍将其绘制在位图内部.

As suggested by @TaW on this previous question, I setted PenAlignment.Inset to draw the circle inside the Bitmap, but this caused another problem.


I want to draw a circle on a specified Bitmap with antialiasing.

SmoothingMode.AntiAlias

The problem is that, when I use PenAlignment.Inset, the antialiasing doesn't work correctly!
Instead, with PenAlignment.Center, it works correctly...
Any suggestion to resolve this problem?

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;


(Note the bugs on the left image)

解决方案

Deflating the bounding rectangle by 1/2 of the pen's stroke width should solve this problem. By "deflate", I mean pull in all 4 sides towards the rectangle's center by 1/2 pen width:

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

or plugging in a hardcoded pen width of 4:

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

Note that the full pen width must be subtracted from the rectangle's width and height in order to pull the right and bottom sides in by 1/2 pen width while keeping the rectangle centered on the same point.

Using this code with pen alignment centered, 1/2 of the stroke width will be drawn outside of the rectangle at the points where the ellipse touches the rectangle, but it will still be drawn inside the bitmap.

这篇关于DrawEllipse:用PenAlignment.Inset破坏抗锯齿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆