Reputation: 3288
Is it possible to get all the points of the objects we have drawn using Graphics object. For example I have drawn an ellipse using Collapse
Graphics g = this.GetGraphics();
g.DrawEllipse(Pens.Red, 300, 300, 100, 200);
Than How I can get all the points or pixels drawn by that function Or is it possible to get all the points or pixels drawn on form.
Thanks……….
Upvotes: 1
Views: 3361
Reputation: 32278
To answer the second part of your question, on how to find pixels affected:
I would highly recommend a mathmatical solution, as listed above. However, for a more brute-force
option, you could simply to create a image, draw to it, and then loop through each pixel to find those pixels affected. This will work, but will be very slow as compared to a genuine mathmatical solution. It will grow slower as the size of the image increases.
This will not work if you antialiasis your drawn circle, whereas their may be shading and transparency. It will however work for what you have listed above.
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;
}
Upvotes: 1
Reputation: 81690
There is no built-in function - as far as I know. But calculating all those points would not be difficult as you just need to have a function to calculate according to the function of the shape.
Ellipse has this function, you can just put X values from start to end and calculate Y from it which should give you all points:
Upvotes: 3