System.Drawing.Graphics

时间:2011-03-14 13:44:31

标签: c# winforms graphics

是否可以使用Graphics对象获取我们绘制的对象的所有点。 例如,我使用了椭圆  崩

Graphics g = this.GetGraphics();
g.DrawEllipse(Pens.Red, 300, 300, 100, 200);

比 如何获得该函数绘制的所有点或像素 或者是否可以获得表格上绘制的所有点或像素。

感谢.........。

2 个答案:

答案 0 :(得分:3)

据我所知,没有内置功能。但是计算所有这些点并不困难,因为你只需要根据形状的函数来计算函数。

Ellipse有这个功能,你可以从头到尾放置X值并从中计算Y值,它可以给你所有点:

enter image description here

答案 1 :(得分:1)

要回答问题的第二部分,请查看如何查找受影响的像素:

我会高度推荐一个数学解决方案,如上所列。但是,对于更多brute-force选项,您可以简单地创建图像,绘制图像,然后遍历每个像素以查找受影响的像素。这个工作,但与真正的数学解决方案相比,非常慢。随着图像尺寸的增加,它会变慢。

如果您对绘制的圆圈进行抗病毒处理,这将工作,而它们可能是阴影和透明度。但它适用于您上面列出的内容。

e.g。

...

List<Point> points = CreateImage(Color.Red,600,600);

...

    private List<Point> CreateImage(Color drawColor, int width, int height)
    {
        // Create new temporary bitmap.
        Bitmap background = new Bitmap(width, height);

        // Create new graphics object.
        Graphics buffer = Graphics.FromImage(background);

        // Draw your circle.
        buffer.DrawEllipse(new Pen(drawColor,1), 300, 300, 100, 200);

        // Set the background of the form to your newly created bitmap, if desired.
        this.BackgroundImage = background;

        // Create a list to hold points, and loop through each pixel.
        List<Point> points = new List<Point>();
        for (int y = 0; y < background.Height; y++)
        {
            for (int x = 0; x < background.Width; x++)
            {
                // Does the pixel color match the drawing color?
                // If so, add it to our list of points.
                Color c = background.GetPixel(x,y);
                if (c.A == drawColor.A &&
                    c.R == drawColor.R &&
                    c.G == drawColor.G &&
                    c.B == drawColor.B)
                {
                    points.Add(new Point(x,y));
                }
            }
        }
        return points;
    }
相关问题