다음을 통해 공유


방법: 오프스크린으로 이미지 그리기

업데이트: 2007년 11월

폼과 연결되지 않은 Graphics 개체를 사용하여 오프스크린으로 이미지를 만들면 큰 이미지를 그릴 때 깜빡임 현상을 줄일 수 있습니다. 그런 다음 폼의 Graphics 개체를 사용하여 화면에서 이미지를 그립니다.

예제

이 예제에서는 비트맵에서 파생된 Graphics 개체를 사용하여 큰 비트맵을 오프스크린으로 만들도록 OnPaint 메서드를 재정의합니다. 그런 다음 PaintEventArgsGraphics 속성에서 반환된 Graphics 개체를 사용하여 화면에 비트맵을 그립니다.

폼이 로드된 후 이미지가 나타나는 데 몇 초 정도의 시간이 소요될 수 있습니다.

Protected Overrides Sub OnPaint(e As PaintEventArgs)

    Dim bmp As Bitmap
    Dim gOff As Graphics

    ' Create a bitmap the size of the form.
    bmp = New Bitmap(ClientRectangle.Width, ClientRectangle.Height)

    Dim BlueBrush As New SolidBrush(Color.Blue)
    Dim WhitePen As New Pen(Color.White, 3)

    ' Create a Graphics object that is not on the form.
    gOff = Graphics.FromImage(bmp)
    gOff.FillRectangle(new SolidBrush(color.red), 0, 0, _
        bmp.Width, bmp.Height)

    ' Draw a complex bitmap of 1000 random rectangles. It will take a few
    ' seconds to draw.
    Dim z As Integer
    For z = 1 To 1000

        ' Generate a random number with
        ' seeds from the system clock.
        Thread.Sleep(1)
        Dim rx As New Random()
        Thread.Sleep(1)
        Dim ry As New Random()

        ' Create rectangles in the inner area of the form.
        Dim rect As New Rectangle(rx.Next(10,200), ry.Next(10,200), 10, 10)
        gOff.DrawRectangle(WhitePen, rect)
        gOff.FillRectangle(BlueBrush, rect)
    Next z

    ' Use the Graphics object from 
    ' PaintEventArgs to draw the bitmap onto the screen.
    e.Graphics.DrawImage(bmp, 0, 0, ClientRectangle, GraphicsUnit.Pixel)

    gOff.Dispose()

End Sub
protected override void OnPaint(PaintEventArgs e)
{

    Bitmap bmp;
    Graphics gOff;

    // Create a bitmap the size of the form.
    bmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);

    SolidBrush BlueBrush = new SolidBrush(Color.Blue);
    Pen WhitePen = new Pen(Color.White,3);

    // Create a Graphics object that is not on the form.
    gOff = Graphics.FromImage(bmp);
    gOff.FillRectangle(new SolidBrush(Color.Red), 0, 0, 
        bmp.Width, bmp.Height);

    // Draw a complex bitmap of 1000 random rectangles. It will take a few
    // seconds to draw.
    for (int z = 1; z <= 1000; z++)
    {
        // Generate a random number with
        // seeds from the system clock.
        Thread.Sleep(1);
        Random rx = new Random();
        Thread.Sleep(1);
        Random ry = new Random();

        // Create rectangles in the inner area of the form.
        Rectangle rect = new Rectangle(rx.Next(10,200), ry.Next(10,200),
            10, 10);
        gOff.DrawRectangle(WhitePen, rect);
        gOff.FillRectangle(BlueBrush, rect);
    }

    // Use the Graphics object from 
    // PaintEventArgs to draw the bitmap onto the screen.
    e.Graphics.DrawImage(bmp, 0, 0, ClientRectangle, GraphicsUnit.Pixel);
    gOff.Dispose();
}

코드 컴파일

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

강력한 프로그래밍

오프스크린 그리기용으로 만들어진 Graphics 개체는 삭제해야 합니다. PaintEventArgs 개체의 Graphics 속성에서 반환한 Graphics 개체는 가비지 수집기를 통해 소멸되므로 명시적으로 삭제할 필요가 없습니다.

참고 항목

기타 리소스

.NET Compact Framework의 그래픽 및 그리기