>  기사  >  백엔드 개발  >  C# GDI+ 기술

C# GDI+ 기술

高洛峰
高洛峰원래의
2016-12-17 10:01:341351검색

GDI+ 개요

GDI+는 이전 버전의 Windows에 포함된 그래픽 장치 인터페이스인 GDI의 후속 제품입니다. Windows XP 운영 체제의 하위 시스템을 구성하는 API(응용 프로그래밍 인터페이스)입니다. GDI+ 기본 클래스의 기본 네임스페이스 및 설명: System.드로잉--기본 그리기 기능과 관련된 대부분의 클래스, 구조, 열거형 및 대리자를 포함합니다. System.드로잉.드로잉2D - 앤티앨리어싱, 지오메트리 변환, 그래픽 경로를 포함한 대부분의 고급 2D 및 벡터 드로잉 작업을 지원합니다. System.드로잉.이미징--이미지(비트맵, GIF 파일 등) 처리에 도움이 되는 다양한 클래스입니다. System.드로잉.인쇄--프린터나 인쇄 미리 보기 창을 출력 장치로 사용할 때 사용되는 클래스입니다. 시스템.드로잉.디자인--디자인 중 사용자 인터페이스 확장과 관련된 미리 정의된 여러 대화 상자, 속성 시트 및 기타 사용자 인터페이스 요소입니다. System.드로잉.Text – 글꼴 및 글꼴 모음에 대한 고급 작업을 수행하는 클래스입니다.

기본 그래픽 그리기

Graphics 클래스는 GDI+의 핵심입니다. Graphics 개체는 GDI+ 그리기 화면을 나타내며 디스플레이 장치에 개체를 그리는 방법을 제공합니다. Graphics 클래스는 직선, 곡선, 그래픽, 이미지 및 텍스트를 그리는 방법을 캡슐화합니다. 이는 직선, 곡선, 그래픽, 이미지 및 텍스트를 그리는 데 GDI+에서 사용되는 클래스입니다.

직선 그리기

Graphics 클래스의 DrawLine 메서드는 오버로드될 수 있으며 주로 좌표 쌍으로 지정된 두 점을 연결하는 선을 그리는 데 사용됩니다. (1) 두 개의 Point 구조체를 연결하는 선을 그립니다.

public void DrawLine(Pen pen, Point pt1,Point pt2)

펜: 펜 개체로 선 색상, 너비 및 스타일을 결정합니다. pt1:연결될 첫 번째 점을 나타내는 점 구조. pt2:연결할 두 번째 점을 나타내는 점 구조. (2) 좌표쌍으로 지정된 두 점을 연결하는 선을 그립니다.

Public void DrawLine(Pen pen,int x1,int y1,int x2,int y2)

직선 그리기 샘플 코드:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Blue, 2);
    graphics.DrawLine(myPen, 50, 30, 170, 30);
}

직사각형 그리기

Graphics 클래스의 DrawRectangle 메서드는 오버로드될 수 있습니다. (1) Rectangle 구조로 지정된 직사각형을 그립니다.

public void DrawRectangle(Pen pen,Rectangle rect)

펜: 펜 개체로 선 색상, 너비 및 스타일을 결정합니다. ret: 직사각형을 그리는 Rectangle 구조체를 나타냅니다. 예:

Rectangle rect = new Rectangle(0, 0, 80, 50);

(2) 너비와 높이의 좌표 쌍으로 지정된 직사각형을 그립니다.

public void DrawRectangle(Pen pen, int x, int y, int width, int height)

펜: 펜 개체로 선 색상, 너비 및 스타일을 결정합니다. x: 그릴 사각형의 왼쪽 위 모서리의 x 좌표입니다. y: 그릴 사각형의 왼쪽 위 모서리의 y 좌표입니다. 너비와 높이는 각각 너비와 높이를 나타냅니다. 직사각형 그리기를 위한 샘플 코드:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Blue, 2);
    graphics.DrawRectangle(myPen, 70, 20, 80, 50);
}

타원 그리기

Graphic 클래스의 DrawEllipse 메소드(오버로드 가능) 주로 Rectangle 구조에 의해 지정된 경계가 있는 타원을 그리는 데 사용됩니다. (1) Rectangle 구조로 지정된 경계를 사용하여 타원을 그립니다.

public void DrawEllipse(Pen pen, Rectangle rect)

(2) 테두리로 정의된 타원을 그립니다(테두리는 좌표, 높이, 너비의 쌍으로 지정됩니다).

public void DrawEllipse(Pen pen, int x, int y, int width, int height)

타원 그리기 샘플 코드:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Blue, 3);
    Rectangle myRectangle = new Rectangle(70, 20, 100, 60);
    graphics.DrawEllipse(myPen, myRectangle);
}

그리기 Graphics 클래스의 Circle Arc

DrawArc 메서드는 오버로드될 수 있습니다. (1) Rectangle 구조로 지정된 타원의 일부를 나타내는 호를 그립니다.

public void DrawArc(Pen pen, Rectangle rect, float startAngle, float sweepAngle)

펜: 펜 개체로 선 색상, 너비 및 스타일을 결정합니다. ret: 타원의 경계를 정의하는 직사각형 구조입니다. startAngle: x축에서 호의 시작점까지 시계 방향으로 측정된 각도(도)입니다. SweepAngle: startAngle 매개변수에서 호의 끝점까지 시계 방향으로 측정된 각도(도)입니다. (2) 좌표, 너비 및 높이의 쌍으로 지정된 타원 부분을 나타내는 호를 그립니다.

public void DrawArc(Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle)

호 그리기 예제 코드:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Blue, 5);
    Rectangle myRectangle = new Rectangle(70, 20, 100, 60);
    graphics.DrawArc(myPen, myRectangle,210,120);
}

다각형 그리기

에는 Graphics 개체, Pen 개체 및 Point(또는 PointF) 개체 배열이 필요합니다. Graphics 클래스는 DrawPolygon 메서드를 제공합니다. Pen 개체는 너비, 색상 등 다각형을 렌더링하는 데 사용되는 선 특성을 저장하고 Point(또는 PointF) 개체 배열은 다각형의 개별 정점을 저장합니다. 다시 로드할 수 있습니다. (1) 일련의 Point 구조로 정의된 다각형을 그립니다.

public void DrawPolygon(Pen pen, Point[] pints)

(2) PointF 구조 집합으로 정의된 다각형을 그립니다.

public void DrawPolygon(Pen pen, PointF[] pints)

다각형 그리기 샘플 코드:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Red, 5);
    Point point1 = new Point(80, 20);
    Point point2 = new Point(40, 50);
    Point point3 = new Point(80, 80);
    Point point4 = new Point(160, 80);
    Point point5 = new Point(200, 50);
    Point point6 = new Point(160, 20);
    Point[] myPoints = { point1, point2, point3, point4, point5, point6 };
    graphics.DrawPolygon(myPen, myPoints);
}

그리기 기본 샘플 바

카디널 스플라인은 일련의 개별 곡선이 연결되어 더 큰 곡선을 형성하는 것입니다. 점 배열과 장력 매개변수로 지정된 스플라인은 날카로운 모서리나 곡선의 가파른 변화 없이 배열의 각 점을 부드럽게 통과합니다. (1) 지정된 포인트 구조 세트를 통과하는 기본 스플라인을 그립니다.

public void DrawCurve(Pen pen, Point[] points)

(2) 지정된 장력을 사용하여 지정된 포인트 구조 세트를 통과하는 카디널 스플라인을 그립니다.

public void DrawCurve(Pen pen, Point[] points, float tension)

장력: 0.0F보다 크거나 같은 값, 곡선의 장력을 지정합니다. (3) 배열의 시작을 기준으로 한 오프셋에서 시작하여 지정된 PointF 구조 세트를 통해 기본 스플라인을 그립니다.

public void DrawCurve(Pen pen, Point[] points, int offset, int numberOfSegments)

offset:从points参数数组中的第一个元素到曲线中起始点的偏移量。numberOfSegments:起始点之后要包含在曲线中的段数。 (4)使用指定张力,绘制经过一组指定Point结构的基数样条。

public void DrawCurve(Pen pen, Point[] points, int offset, int numberOfSegments, float tension)

   

绘制基数样条示例代码:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Red, 5);
    Point point1 = new Point(50, 20);
    Point point2 = new Point(60, 30);
    Point point3 = new Point(70, 25);
    Point point4 = new Point(100, 50);
    Point point5 = new Point(130, 30);
    Point point6 = new Point(150, 45);
    Point[] myPoints = { point1, point2, point3, point4, point5, point6 };
    graphics.DrawCurve(myPen, myPoints, 1.0F);
}

   

绘制贝赛尔样条

贝塞尔样条是由4个点指定的曲线:两个端点(p1,p2)和两个控制点(c1,c2)。曲线开始于p1,结束于p2。曲线不经过控制点,但是控制点像磁铁一样,在某些方向上拉伸曲线并影响曲线弯曲的方式。 调用Graphics类的DrawBezier方法,可重载。 (1)绘制由4个Point结构定义的贝塞尔样条。

public void DrawBezier(Pen pen, Point pt1, Point pt2, Point pt3, Point pt4)

   

4个Point点分别表示起始点、第一个控制点、第二个控制点和结束点。
(2)绘制由4个表示点的有序坐标对定义的贝塞尔样条。

public void DrawBezier(Pen pen, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)

   

x2,y2及x3,y3分别表示第1个、第2个控制点相应坐标。顺序和第一种方法类似。
绘制贝塞尔样条示例代码:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Red, 5);
    float startX = 50.0F;
    float startY = 80.0F;
    float controlX1 = 150.0F;
    float controlY1 = 20.0F;
    float controlX2 = 230.0F;
    float controlY2 = 50.0F;
    float endX = 190.0F;
    float endY = 80.0F;
    graphics.DrawBezier(myPen, startX, startY, controlX1, controlY1, controlX2, controlY2, endX, endY);
}

   

绘制图形路径

路径是通过组合直线、矩形和简单的曲线而形成的。在GDI+中,GraphicsPath对象允许将基本构造块收集到一个单元中,调用一次Graphics类的DrawPath方法,就可以绘制出整个单元的直线、矩形、多边形和曲线。

public void DrawPath(Pen pen, GraphicsPath path)

   

pen:Pen对象,确定线条颜色、宽度和样式。path:要绘制的GraphicsPath图形路径。 PS:注意要引用System.Drawing.Drawing2D命名空间。
绘制图形路径示例代码:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    GraphicsPath myGraphicsPath = new GraphicsPath();
    Pen myPen = new Pen(Color.Blue, 1);
    Point[] myPoints = { new Point(15, 30), new Point(30, 40), new Point(50, 30) };
    myGraphicsPath.AddArc(15, 20, 80, 50, 210, 120);
    myGraphicsPath.StartFigure();
    myGraphicsPath.AddCurve(myPoints);
    myGraphicsPath.AddString("图形路径", new FontFamily("华文行楷"), (int)FontStyle.Underline, 50, new PointF(20, 50), new StringFormat());
    myGraphicsPath.AddPie(180,20,80,50,210,120);
    graphics.DrawPath(myPen, myGraphicsPath);
}

   


更多C# GDI+技术相关文章请关注PHP中文网!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.