다음을 통해 공유


방법: 폼에 배경 이미지 설정

업데이트: 2007년 11월

폼의 OnPaint 메서드를 재정의하여 이미지를 폼의 배경으로 그릴 수 있습니다.

폼에 배경 이미지를 그리려면

  1. 폼의 OnPaint 메서드를 재정의합니다.

  2. 장치의 외부 파일에 있는 이미지 또는 어셈블리의 포함 리소스로 컴파일된 이미지를 가져옵니다.

  3. PaintEventArgsGraphics 속성에서 Graphics 개체를 사용하여 이미지를 그립니다. 이 때 폼의 ClientRectangle 속성에서 지정된 크기를 사용합니다.

예제

이 예제에서는 포함 리소스로 컴파일된 이미지 파일을 폼의 배경 이미지로 사용합니다.

Protected Overrides Sub OnPaint(e As PaintEventArgs)

    ' Get image compiled as an embedded resource.
    Dim asm As Assembly = Assembly.GetExecutingAssembly()
    Dim backGroundImage As New Bitmap(asm.GetManifestResourceStream("mypicture.bmp"))

    e.Graphics.DrawImage(backgroundImage, Me.ClientRectangle, _
        New Rectangle(0, 0, backgroundImage.Width, backgroundImage.Height), _
        GraphicsUnit.Pixel)

End Sub   
protected override void OnPaint(PaintEventArgs e)
{        
    // Get image compiled as an embedded resource.
    Assembly asm = Assembly.GetExecutingAssembly();
    Bitmap backgroundImage = new Bitmap(asm.GetManifestResourceStream("mypicture.jpg"));

    e.Graphics.DrawImage(backgroundImage, this.ClientRectangle,
        new Rectangle(0,0, backgroundImage.Width, backgroundImage.Height),
        GraphicsUnit.Pixel);
}

코드 컴파일

이 예제에는 다음과 같은 네임스페이스에 대한 참조가 필요합니다.

참고 항목

개념

사용자 지정 컨트롤 개발

기타 리소스

.NET Compact Framework의 Windows Forms 컨트롤