DrawEllipse:椭圆超出位图大小 [英] DrawEllipse: Ellipse goes outside of the Bitmap size

查看:112
本文介绍了DrawEllipse:椭圆超出位图大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在指定的Bitmap上用DrawEllipse绘制一个与位图大小相同的圆,但是结果是该圆看起来像是在边缘被剪裁了.
为什么会出现这个问题?

I want to draw a circle with DrawEllipse on a specified Bitmap, with the same size of the Bitmap, but the result is that the circle appears clipped at the edges.
Why this problem?

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;

推荐答案

默认情况下,一支笔具有

By default a Pen has a PenAlignment.Center.

这意味着它的一半宽度将在边界矩形的外部绘制.

This means that half of its widh will draw outside the bounding rectangle.

只需将其更改为PenAlignment.Inset即可避免该问题:

You can simply avoid the issue by changing it to PenAlignment.Inset:

using (Pen p = new Pen(Color.Black, 4) { Alignment = PenAlignment.Inset})
{
    g.DrawEllipse(p, new Rectangle(0, 0, layer.Width, layer.Height));
}

更新:如果要为Graphics对象打开平滑功能,则在笔划的两侧需要增加1或2个像素作为抗锯齿像素.现在无法避免使用较小的边界矩形.但是..:

Update: If you want to turn on smoothing for the Graphics object you will need 1 or 2 extra pixels on both sides of the pen stroke for the anti-aliasing pixels. Using a smaller bounding rectanlge can't be avoided now. But..:

Rectangle rect = new Rectangle(Point.Empty, layer.Size);
rect.Inflate(-1, -1);  // or -2

..应该做..

这篇关于DrawEllipse:椭圆超出位图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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