意外结果绘制圆圈

本文关键字:绘制 结果 意外 | 更新日期: 2023-09-27 18:35:28

我正在尝试通过在旧圆的顶部绘制一个像素大半径的圆来绘制一个变大尺寸的圆,从而在pictureBox上创建一个不断增长的圆。

我看到的是一个水滴形的图形,而不是一个圆圈。我使用的代码是:

        for (int x = 0; x < 20; x++)
        {
            System.Drawing.Graphics graphics = box.CreateGraphics();
            System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(xpos-10, ypos-10, x, x);
            graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
        }

我错过了什么?

意外结果绘制圆圈

矩形 x/y 是包含椭圆的矩形的左上角。如果绘制较大的圆,则还需要移动边界矩形。

        for (int x = 0; x < 20; x++)
        {
            System.Drawing.Graphics graphics = e.Graphics;
            System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(xpos - 10 - x / 2, ypos - 10 - x / 2, x, x);
            graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
        }

如果您希望圆是等心的,则需要将矩形的XY设置为左侧。

因此:

Rectangle rectangle = new Rectangle(xpos-10-x/2, ypos-10-y/2, x, x);

另一方面,你不会看到圆圈增长。由于PictureBox仅在所有绘制完成后交换缓冲区。您需要的是一个刷新事件,并利用这段时间来确定下一个圆圈的大小。

你在矩形内画圆,当你只增加矩形的高度和宽度时,你也会将中心向右和向下移动。此外,重复使用图形以获得更好的性能

using (var graphics = box.CreateGraphics())
{
    for (int x = 0; x < 20; x++)
    {
        System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(xpos-10-x/2, ypos-10-x/2, x, x);
        graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
    }
}