다음을 통해 공유


방법: 이미지 복사

업데이트: 2007년 11월

.NET Compact Framework에서는 Image.Clone 메서드를 지원하지 않지만 이미지 또는 이미지 일부를 복사할 수 있습니다. 아래 예제에서는 다음과 같은 작업을 수행하는 방법을 보여 줍니다.

  • 비트맵을 만드는 메서드를 정의합니다.

  • 비트맵 또는 비트맵 일부를 복사하는 오버로드된 메서드를 정의합니다.

  • 폼의 OnPaint 메서드를 재정의하여 이러한 메서드를 호출하고 화면에 이미지를 그립니다.

비트맵을 만들려면

  • 이 메서드는 데모용 비트맵을 만듭니다.
' Creates a bitmap for copying.
Function CreateBitmap(sideSize As Integer) As Bitmap
    Dim bmp As New Bitmap(sideSize, sideSize)
    Dim g As Graphics = Graphics.FromImage(bmp)

    g.FillEllipse(New SolidBrush(Color.Red), 0, 0, sideSize, sideSize)
    g.DrawLine(New Pen(Color.Black), 0, 0, sideSize, sideSize)
    g.DrawLine(New Pen(Color.Black), sideSize, 0, 0, sideSize)
    g.Dispose()

    Return bmp
End Function
// Creates a bitmap for copying.
private Bitmap CreateBitmap(int sideSize)
{
    Bitmap bmp = new Bitmap(sideSize, sideSize);
    Graphics g = Graphics.FromImage(bmp);

    g.FillEllipse(new SolidBrush(Color.Red), 0, 0, sideSize, sideSize);
    g.DrawLine(new Pen(Color.Black), 0, 0, sideSize, sideSize);
    g.DrawLine(new Pen(Color.Black), sideSize, 0, 0, sideSize);
    g.Dispose();

    return bmp;
}

비트맵을 복제하려면

  • 이 메서드 오버로드에서는 소스 비트맵을 매개 변수로 사용하고 비트맵을 복사본으로 반환합니다.
' Copies the entire bitmap.
Overloads Function CopyBitmap(source As Bitmap) As Bitmap
    Return New Bitmap(source)
End Function
// Copies the entire bitmap.
protected Bitmap CopyBitmap(Bitmap source)
{
    return new Bitmap(source);
}

비트맵의 일부를 복사하려면

  • 이 메서드 오버로드는 Rectangle을 매개 변수로 사용하여 반환할 비트맵 부분의 크기를 결정합니다.
' Copies a part of the bitmap.
Overloads Function CopyBitmap(source As Bitmap, part As Rectangle) As Bitmap
    Dim bmp As New Bitmap(part.Width, part.Height)

    Dim g As Graphics = Graphics.FromImage(bmp)
    g.DrawImage(source, 0, 0, part, GraphicsUnit.Pixel)
    g.Dispose()

    Return bmp
End Function
// Copies a part of a bitmap.
protected Bitmap CopyBitmap(Bitmap source, Rectangle part)
{
    Bitmap bmp = new Bitmap(part.Width, part.Height);
    Graphics g = Graphics.FromImage(bmp);
    g.DrawImage(source,0,0,part,GraphicsUnit.Pixel);
    g.Dispose();
    return bmp;
}

비트맵을 만들고 복사하고 그리려면

  • OnPaint 메서드 오버로드는 비트맵을 만드는 메서드를 호출한 다음 비트맵의 일부를 복제합니다. 또한 복제된 비트맵을 파일로 저장합니다.
' Draws the bitmaps on the form.   
Protected Overrides Sub OnPaint(e As PaintEventArgs)
    Dim arialFont As Font
    Dim blackBrush As Brush

    arialFont = New Font("Arial", 10, FontStyle.Regular)
    blackBrush = New SolidBrush(Color.Black)

    ' Set the size of the sides of the bitmap,
    ' and get one-third of it for the center bitmap.
    Dim sidesize As Integer = 75
    Dim third As Integer = CInt(sidesize / 3)

    ' Create bitmap.
    Dim source As Bitmap = CreateBitmap(sidesize)

    ' Copy entirely as a clone.
    Dim clone As Bitmap = CopyBitmap(source)

    ' Copy the center part of the bitmap.
    Dim center As Bitmap = _ 
        CopyBitmap(source, New Rectangle(third, third, third, third))

    ' Save the bitmap to a file.
    clone.Save("newbitmap.bmp", ImageFormat.Bmp)

    ' Draw the source, clone, and partial 
    ' bitmaps vertically down the screen. 
    Dim y As Integer = 10

    e.Graphics.DrawString("source bitmap:", ArialFont, BlackBrush, 10, y)
    y += 20

    e.Graphics.DrawImage(source, 10, y)
    y += source.Height + 10

    e.Graphics.DrawString("clone bitmap:", ArialFont, BlackBrush, 10, y)
    y += 20

    e.Graphics.DrawImage(clone, 10, y)
    y += clone.Height + 10

    e.Graphics.DrawString("center part of bitmap:", ArialFont, BlackBrush, 10, y)
    y += 20

    e.Graphics.DrawImage(center, 10, y)
    y += center.Height + 10

    ' Dispose graphic objects.
    arialFont.Dispose()
    blackBrush.Dispose()

End Sub
// Draws the bitmaps on the form.   
protected override void OnPaint(PaintEventArgs e)
{
    Font arialFont;
    Brush blackBrush;
    arialFont = new Font("Arial", 10, FontStyle.Regular);
    blackBrush = new SolidBrush(Color.Black);

    // Set the size of the sides of the bitmap,
    // and get one-third of it for the center bitmap.
    int sidesize = 75;
    int third = (int) sidesize/3;

    // Create bitmap.
    source = CreateBitmap(sidesize);

    // Copy entirely as a clone.
    clone = CopyBitmap(source);

    // Copy the center part of the bitmap.
    center = CopyBitmap(source, new Rectangle(third, third, third, third));

    // Save the bitmap to a file.
    clone.Save("newbitmap.bmp", ImageFormat.Bmp);

    // Draw the source, clone, and partial 
    // bitmaps vertically down the screen. 
    int y = 10;

    e.Graphics.DrawString("source bitmap:", arialFont, blackBrush, 10, y);
    y += 20;

    e.Graphics.DrawImage(source, 10, y);
    y += source.Height + 10;

    e.Graphics.DrawString("clone bitmap:", arialFont, blackBrush, 10, y);
    y += 20;

    e.Graphics.DrawImage(clone, 10, y);
    y += clone.Height + 10;

    e.Graphics.DrawString("center part of bitmap:", arialFont, blackBrush, 10, y);
    y += 20;

    e.Graphics.DrawImage(center, 10, y);
    y += center.Height + 10;

    // Dispose graphic objects.
    arialFont.Dispose();
    blackBrush.Dispose();
}

코드 컴파일

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

강력한 프로그래밍

FontBrush 개체는 OnPaint 메서드 오버로드에서 명시적으로 삭제됩니다. PaintEventArgs 개체의 Graphics 속성에서 반환한 Graphics 개체는 가비지 수집기를 통해 소멸되므로 명시적으로 삭제할 필요가 없습니다.

참고 항목

기타 리소스

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