图形对象==> System.Drawing.Graphics [英] Graphics Object==> System.Drawing.Graphics

查看:96
本文介绍了图形对象==> System.Drawing.Graphics的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,
是否可以使用Graphics对象获取我们绘制的对象的所有点.
例如,我使用
绘制了一个椭圆

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……….

推荐答案

否.除非您将它们绘制到空白的新图像上,然后逐像素检查图像,否则不会这样.
如果将其绘制在表单上,​​则可能还会存在其他内容,这将使您难以确定椭圆的一部分,哪些不是椭圆.

您要做什么需要它?
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?


您可以使用 ^ ].

使用其方法,只需将您的工程图添加到路径即可.
之后,您可以使用 PathPoints属性 [ ^ ] GraphicsPath.可能您可以将此属性与 GraphicsPath.PathTypes 一起使用[ ^ ].但这并不能为您提供所有要点,尤其是在指定填充方式的情况下.

正如 OriginalGriff 所建议的那样,所有绘制点都可以使用空白图像,然后再进行分析...您的确切目的是什么?


顺便说一句:您可以使用以下方法绘制路径: Graphics.DrawPath方法 [ ^ ]. :)
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[^]. :)



图形路径点数组将仅返回路径中的优化/广义点.

您可以在哪里使用如下所示的椭圆绘图点生成自己的点.

startX和startY是椭圆的中心点

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

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);



在上面的代码中,我只是使用图形路径来演示椭圆在显示中的方式.但是这里不需要图形路径.您将获得360点对(endX,endY).



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).

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);

}



这样就得到了360像素坐标.使用Bitmap/图像上的getPixel()方法,可以获得椭圆外围的像素.

将度转换为弧度,反之亦然的功能



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

private double deg2rad(double deg)
 {
     return Math.PI * deg / 180;
 }
 private double rad2deg(double rad)
 {
     return rad * (180 / Math.PI);
 }


这篇关于图形对象==&gt; System.Drawing.Graphics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆