我想用DrawEllipse在指定的Bitmap上绘制一个与位图相同大小的圆,但结果是该圆的边缘显示为剪裁。
为什么会出现这个问题?
Bitmap layer = new Bitmap(80, 80);
using (Graphics g = Graphics.FromImage(layer))
{
using (Pen p = new Pen(Color.Black, 4))
{
g.DrawEllipse(p, new Rectangle(0, 0, layer.Width, layer.Height));
}
}
pictureBox3.Size = new Size(100, 100);
pictureBox3.Image = layer;

发布于 2018-05-29 07:29:43
默认情况下,画笔具有PenAlignment.Center。
这意味着其宽度的一半将绘制在边界矩形之外。
您可以通过将其更改为PenAlignment.Inset来避免该问题
using (Pen p = new Pen(Color.Black, 4) { Alignment = PenAlignment.Inset})
{
g.DrawEllipse(p, new Rectangle(0, 0, layer.Width, layer.Height));
}

更新:如果你想打开图形对象的平滑效果,你需要在笔触的两边增加1到2个像素作为抗锯齿像素。使用较小的边界矩形现在是无法避免的。但是……
Rectangle rect = new Rectangle(Point.Empty, layer.Size);
rect.Inflate(-1, -1); // or -2..should do..
https://stackoverflow.com/questions/50574457
复制相似问题