방법: 투명하게 이미지 그리기

업데이트: 2007년 11월

.NET Compact Framework에서는 투명성을 지원하지만 투명 색을 하나만 사용할 수 있습니다. SetColorKey(Color, Color) 메서드는 하위/상위 색상 범위에 지정된 것과 같은 색을 사용해야 합니다.

예제

이 예제에서는 빨간색과 검정색 디자인이 있는 사각형의 Bitmap을 만들고 투명성을 설정하는 두 가지 기술을 보여 줍니다.

  • 이미지의 픽셀을 기반으로 SetColorKey(Color, Color) 메서드를 사용합니다. 이 예제에서는 이미지의 왼쪽 위 픽셀을 사용하여 투명도를 설정합니다. 이 픽셀은 검은색이므로 원래 검은색 픽셀이 모두 투명해집니다.

  • 명시적 색 설정을 기반으로 SetColorKey(Color, Color) 메서드를 사용합니다. 이 예제에서는 색이 빨간색으로 설정되므로 원래 빨간색 픽셀이 모두 투명해집니다.

좀 더 쉽게 이해할 수 있도록 하기 위해 먼저 두 가지 투명도 기법을 모두 주석으로 처리하여 응용 프로그램을 실행한 후 투명도가 설정되지 않은 상태에서 이미지가 어떻게 나타나는지 확인합니다. 그런 다음 각 투명도 기법에 대해 코드의 주석 처리를 제거합니다.

' The .NET Compact Framework supports transparency,
' but with only one transparency color.
' The SetColorKey method must have the same color 
' specified for the low color and high color range.

' Note that you must uncomment lines of code
' as indicated for the desired transparency effect.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

' Create a red and black bitmap to demonstrate transparency.
    Dim bmp As New Bitmap(75, 75)
    Dim g As Graphics = Graphics.FromImage(bmp)

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


Dim attr As New ImageAttributes

' Set the transparency color key based on the upper-left pixel 
' of the image.
' Uncomment the following line to make all black pixels transparent:
' attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0))

' Set the transparency color key based on a specified value.
' Uncomment the following line to make all red pixels transparent:
' attr.SetColorKey(Color.Red, Color.Red)

' Draw the image using the image attributes.
Dim dstRect As New Rectangle(0, 0, bmp.Width, bmp.Height)
e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height, _
    GraphicsUnit.Pixel, attr)

End Sub
// The .NET Compact Framework supports transparency,
// but with only one transparency color.
// The SetColorKey method must have the same color 
// specified for the low color and high color range.

// Note that you must uncomment lines of code
// as indicated for the desired transparency effect.

protected override void OnPaint(PaintEventArgs e)
{
    // Create a red and black bitmap to demonstrate transparency.
    Bitmap bmp = new Bitmap(75,75);
    Graphics g = Graphics.FromImage(bmp);

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

    ImageAttributes attr = new ImageAttributes();

    // Set the transparency color key based on the upper-left pixel 
    // of the image.
    // Uncomment the following line to make all black pixels transparent:
    // attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0));

    // Set the transparency color key based on a specified value.
    // Uncomment the following line to make all red pixels transparent:
    // attr.SetColorKey(Color.Red, Color.Red);

    // Draw the image using the image attributes.
    Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height,
        GraphicsUnit.Pixel, attr);
}

코드 컴파일

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

강력한 프로그래밍

비트맵을 만드는 데 사용되는 Graphics 개체는 명시적으로 삭제됩니다. PaintEventArgs 개체의 Graphics 속성에서 반환한 Graphics 개체는 가비지 수집기를 통해 소멸되므로 명시적으로 삭제할 필요가 없습니다.

참고 항목

기타 리소스

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