Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello friends,
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
Graphics g = this.GetGraphics(); // here "this"  means "form" on which we drawing graphics
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……….
Posted
Updated 14-Mar-11 21:08pm
v2
Comments
Sergey Alexandrovich Kryukov 14-Mar-11 16:04pm    
What's the type of "this" in this example? I suspect you're making mistake in using proper type to host your drawings.
--SA
Pritesh Aryan 15-Mar-11 3:05am    
sorry i don't have mention what "this" here is..
but now i clear that this means "form" on which we drawing the graphics...

No. Not unless you draw them onto a blank, new image and then examine the image pixel by pixel.
If you draw it on a form, there is likely to be other stuff on it which would make it difficult to work out what is part of the ellipse, and what isn't.

What are you trying to do that needs that?
 
Share this answer
 
Comments
Pritesh Aryan 14-Mar-11 10:17am    
Thank you so much for replying
You can use GraphicsPath Class[^].

Simply add your drawings to the path, using it's methods.
After that you can access the points using PathPoints property[^] of GraphicsPath. Probably you can use this property together with GraphicsPath.PathTypes[^]. But this will not give you all of the points drawn especially if there is specified fill mode.

As OriginalGriff already suggested for all drawn points you can use a blank image and later analysis... What is your exact purpose?


BTW: You can draw the path using: Graphics.DrawPath method[^]. :)
 
Share this answer
 
v3
Comments
Pritesh Aryan 14-Mar-11 10:16am    
Thank you so much for replying..
Espen Harlinn 15-Mar-11 15:34pm    
Nice reply, my 5
Nuri Ismail 16-Mar-11 4:06am    
Thank you, Espen! :)
Hi
the Graphics path points array will return only the optimized/generalized points in the path.

Where as you can generate your own points using the ellipse drawing points as below.

startX and startY are the centre point of the ellipse

C#
double minorAxisWidth = 50;
double majorAxisWidth = 100;
double endX;
double endY;
double endX1;
double endY1;
double startX = 100;
double startY = 100;

GraphicsPath path = new GraphicsPath();

for (int j = 0; j < 360; j++)
{
    endX = startX + minorAxisWidth * Math.Cos(deg2rad(j-1));
    endY = startY + majorAxisWidth * Math.Sin(deg2rad(j-1));
    endX1 = startX + minorAxisWidth * Math.Cos(deg2rad(j ));
    endY1 = startY + majorAxisWidth * Math.Sin(deg2rad(j));
    path.AddLine((int)endX, (int)endY, (int)endX1, (int)endY1);

}

e.Graphics.DrawPath(Pens.Black, path);


In the above code I just used the graphics path to demonstrate how the ellipse in the display. But not necessary of a graphics path here. You will be getting 360 point pairs (endX,endY).

C#
double minorAxisWidth = 50;
double majorAxisWidth = 100;
double endX;
double endY;
double endX1;
double endY1;
double startX = 100;
double startY = 100;

//GraphicsPath path = new GraphicsPath();
PointF[] points = new PointF[360];
for (int j = 0; j < 360; j++)
{
    endX = startX + minorAxisWidth * Math.Cos(deg2rad(j));
    endY = startY + majorAxisWidth * Math.Sin(deg2rad(j));
    //endX1 = startX + minorAxisWidth * Math.Cos(deg2rad(j ));
    //endY1 = startY + majorAxisWidth * Math.Sin(deg2rad(j));
    points[j] = new PointF((float)endX, (float)endY);
    //path.AddLine((int)endX, (int)endY, (int)endX1, (int)endY1);

}


So you got 360 pixel coordinates. Using the getPixel() method on the Bitmap/ image you can get the pixels at the ellipse periphery.

The functions for converting Degree to Radian and vice versa

C#
private double deg2rad(double deg)
 {
     return Math.PI * deg / 180;
 }
 private double rad2deg(double rad)
 {
     return rad * (180 / Math.PI);
 }
 
Share this answer
 
v3
Comments
Espen Harlinn 15-Mar-11 15:35pm    
Good effort, my 5
Albin Abel 15-Mar-11 15:39pm    
Thanks Espen Harlinn

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900