방법: 확대/축소 효과 만들기

업데이트: 2007년 11월

다음 예제 프로그램은 크기가 같은 두 개의 비트맵으로 확대/축소 효과를 시뮬레이션합니다. 여기서 두 번째 비트맵에 첫 번째 비트맵을 확대/축소한 가운데 부분이 들어 있습니다.

예제

' Define bmp and bmpZoom as
' global Bitmap variables for your form.

' Call CreateBitmap and then DefineZoom
' from your form's consructor.

Sub CreateBitmap()
    bmp = New Bitmap(75, 75)
    Dim g As Graphics = Graphics.FromImage(bmp)

    Dim BlueBrush As New SolidBrush(Color.Blue)
    Dim RedBrush As New SolidBrush(Color.Red)

    Dim OuterRect As New Rectangle(0, 0, 200, 200)
    g.FillRectangle(BlueBrush, OuterRect)

    Dim InnerRect As New Rectangle(25, 25, 25, 25)
    g.FillRectangle(RedBrush, InnerRect)

    g.Dispose()
End Sub

Sub ZoomImage()
    bmpZoom = New Bitmap(bmp.Width, bmp.Height)
    Dim g As Graphics = Graphics.FromImage(bmpZoom)

    Dim srcRect As New Rectangle(CInt(bmp.Width / 4), CInt(bmp.Height / 4), _
        CInt(bmp.Width / 2), CInt(bmp.Height / 2))
    Dim dstRect As New Rectangle(0, 0, bmpZoom.Width, bmpZoom.Height)
    g.DrawImage(bmp, dstRect, srcRect, GraphicsUnit.Pixel)
End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    e.Graphics.DrawImage(bmp, 0, 0)
    e.Graphics.DrawImage(bmpZoom, 125, 0)

    bmp.Dispose()
    bmpZoom.Dispose()
    MyBase.OnPaint(e)
End Sub
// Define bmp and bmpZoom as
// global Bitmap variables for your form.

// Call CreateBitmap and then DefineZoom
// from your form's consructor.
void CreateBitmap()
{
    bmp = new Bitmap(75, 75);
    Graphics g = Graphics.FromImage(bmp);
    SolidBrush BlueBrush = new SolidBrush(Color.Blue);
    SolidBrush RedBrush = new SolidBrush(Color.Red);
    Rectangle OuterRect = new Rectangle(0, 0, 200, 200);
    g.FillRectangle(BlueBrush, OuterRect);
    Rectangle InnerRect = new Rectangle(25, 25, 25, 25);
    g.FillRectangle(RedBrush, InnerRect);
    g.Dispose();
}

   private void ZoomImage()
    {
        bmpZoom = new Bitmap(bmp.Width, bmp.Height);
        Graphics g = Graphics.FromImage(bmpZoom);
        int new4W = bmp.Width / 4;
        int new4H = bmp.Height / 4;
        int new2W = bmp.Width / 2;
        int new2H = bmp.Height / 2;
        Rectangle srcRect = new Rectangle(new4W, new4H, new2W, new2H);
        Rectangle dstRect = new Rectangle(0, 0, bmpZoom.Width, bmpZoom.Height);
        g.DrawImage(bmp, dstRect, srcRect, GraphicsUnit.Pixel);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.DrawImage(bmp, 0, 0);
        e.Graphics.DrawImage(bmpZoom, 125, 0);
        base.OnPaint(e);
    }

코드 컴파일

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

참고 항목

개념

.NET Compact Framework 방법 항목

기타 리소스

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