是否可以使用Graphics对象获取我们绘制的对象的所有点。例如,我使用折叠绘制了一个椭圆
Graphics g = this.GetGraphics();
g.DrawEllipse(Pens.Red, 300, 300, 100, 200);然后,我如何才能获得所有的点或像素绘制的那个函数,或者有可能获得所有的点或像素绘制在表单上。
谢谢………。
发布于 2011-03-14 22:24:39
为了回答你的问题的第二部分,关于如何找到受影响的像素:
我会高度推荐一个数学解决方案,如上所列。然而,对于更多的brute-force选项,您可以简单地创建一个图像,绘制到该图像,然后循环遍历每个像素以查找受影响的像素。这是可行的,但与真正的数学解决方案相比,这将是非常慢的。随着图像大小的增加,它的增长速度会变慢。
这将不会工作,如果你反锯齿你绘制的圆,而他们可能是阴影和透明度。但是,它将适用于您上面列出的内容。
例如:
...
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;
}发布于 2011-03-14 21:50:09
据我所知,没有内置的函数。但是计算所有这些点并不困难,因为你只需要有一个函数来根据形状的函数来计算。
Ellipse有这个功能,你可以从头到尾放X值,然后从它计算Y,这应该会给你所有的分数:

https://stackoverflow.com/questions/5299386
复制相似问题