[C#기초] 10. GDI

INDEX

  1. GDI+
  2. Color 구조체
  3. Pen
  4. Brush

GDI+

GDI+

CreateGraphics() 사용

CreateGraphics() 사용 예시

List<Point> ptCircle = new List<Point>();
public Form1()
{
    InitializeComponent();
}

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
    ptCircle.Add(e.Location);

    Graphics g = CreateGraphics();
    foreach (Point x in ptCircle)
    {
        g.DrawEllipse(Pens.Black, x.X - 10, x.Y - 10, 20,20);
    }
    g.Dispose();
}

Color 구조체

Color 구조체

Pen

Pen vs Pens

펜 스타일

Pen을 사용한 DrawLine 예제 코드

public Form1()
{
    InitializeComponent();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Pen newPen = new Pen(Color.FromArgb(200, 150, 100));
    newPen.Width = 4.0f;
    e.Graphics.DrawLine(newPen, 10, 10, 100, 10);
    newPen.Dispose();
}

펜 스타일 적용한 예제 코드

	private void Form1_Paint(object sender, PaintEventArgs e)
	{
	    Pen newPen = new Pen(Color.FromArgb(200, 150, 100));
	    //using System.Drawing.Drawing2D 추가하면     newPen.DashStyle = Dashstyle.DashDot;으로 사용 가능
	    newPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
	    newPen.Width = 4.0f;
	    e.Graphics.DrawLine(newPen, 10, 10, 100, 10);
	    newPen.Dispose();
	}

Brush

역할

브러시 종류

브러시를 요구하는 메서드의 공통점

브러시를 사용한 도형 색칠 예시

private void Form1_Paint(object sender, PaintEventArgs e)
{
    SolidBrush tempBrush = new SolidBrush(Color.Blue);
    e.Graphics.FillEllipse(tempBrush, 10, 10, 100, 100);
    tempBrush.Dispose();
}

결과

image