다음을 통해 공유


방법: 펜 굵기 및 맞춤 설정

업데이트: 2007년 11월

Pen 개체를 만들 때 펜 굵기를 생성자에 대한 인수 중 하나로 지정할 수 있습니다. 또한 Pen 클래스의 Width 속성을 사용하여 펜 굵기를 변경할 수도 있습니다.

이론적인 선의 굵기는 0입니다. 굵기가 1픽셀인 선을 그릴 경우 픽셀은 이론적인 선의 가운데에 위치하게 됩니다. 굵기가 2 픽셀 이상인 선을 그리는 경우에는 픽셀이 이론적인 선의 가운데에 위치하거나 한쪽 옆에 나타납니다. Pen 개체의 펜 맞춤 속성을 설정하면 해당 펜으로 그리는 픽셀의 상대적인 위치를 이론적인 선을 기준으로 결정할 수 있습니다.

다음 코드 예제에 나타나는 Center, OutsetInset 값은 PenAlignment 열거형의 멤버입니다.

다음 코드 예제에서는 선을 두 번 그리는데 한 번은 굵기가 1인 검정 펜을 사용하고 또 한 번은 굵기가 10인 녹색 펜을 사용합니다.

펜 굵기를 변경하려면

  • Alignment 속성의 값을 기본값인 Center로 설정하여 녹색 펜으로 그리는 픽셀이 이론적인 선의 가운데에 위치하도록 지정합니다. 아래 그림에 이 예제에서 그린 선이 나와 있습니다.

    펜

    다음 코드 예제에서는 사각형을 두 번 그리는데 한 번은 굵기가 1인 검정 펜을 사용하고 또 한 번은 굵기가 10인 녹색 펜을 사용합니다.

    Dim blackPen As New Pen(Color.FromArgb(255, 0, 0, 0), 1)
    Dim greenPen As New Pen(Color.FromArgb(255, 0, 255, 0), 10)
    greenPen.Alignment = PenAlignment.Center
    
    ' Draw the line with the wide green pen.
    e.Graphics.DrawLine(greenPen, 10, 100, 100, 50)
    
    ' Draw the line with the thin black pen.
    e.Graphics.DrawLine(blackPen, 10, 100, 100, 50)
    
    
    Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
    Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
    greenPen.Alignment = PenAlignment.Center;
    
    // Draw the line with the wide green pen.
    e.Graphics.DrawLine(greenPen, 10, 100, 100, 50);
    
    // Draw the line with the thin black pen.
    e.Graphics.DrawLine(blackPen, 10, 100, 100, 50);
    

펜 맞춤을 변경하려면

  • Alignment 속성의 값을 Center로 설정하여 녹색 펜으로 그리는 픽셀이 사각형의 경계선 가운데에 위치하도록 지정합니다.

    아래 그림에 이 코드에서 그린 사각형이 나와 있습니다.

    펜

    Dim blackPen As New Pen(Color.FromArgb(255, 0, 0, 0), 1)
    Dim greenPen As New Pen(Color.FromArgb(255, 0, 255, 0), 10)
    greenPen.Alignment = PenAlignment.Center
    
    ' Draw the rectangle with the wide green pen.
    e.Graphics.DrawRectangle(greenPen, 10, 100, 50, 50)
    
    ' Draw the rectangle with the thin black pen.
    e.Graphics.DrawRectangle(blackPen, 10, 100, 50, 50)
    
    
    Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
    Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
    greenPen.Alignment = PenAlignment.Center;
    
    // Draw the rectangle with the wide green pen.
    e.Graphics.DrawRectangle(greenPen, 10, 100, 50, 50);
    
    // Draw the rectangle with the thin black pen.
    e.Graphics.DrawRectangle(blackPen, 10, 100, 50, 50);
    

안쪽 맞춤 펜을 만들려면

  • 위의 코드 예제에서 세 번째 문을 다음과 같이 수정하여 녹색 펜의 맞춤을 변경합니다.

    greenPen.Alignment = PenAlignment.Inset
    
    
    greenPen.Alignment = PenAlignment.Inset;
    

    아래 그림과 같이 굵은 녹색 선의 픽셀이 사각형의 안쪽에 나타납니다.

    펜

참고 항목

기타 리소스

펜을 사용하여 선과 도형 그리기

Windows Forms의 그래픽 및 그리기