Share via


방법: 혼합 모드를 사용하여 알파 혼합 조절

다음과 같은 특징이 있는 오프스크린 비트맵을 만들려는 경우가 있을 수 있습니다.

  • 색에는 255보다 작은 알파 값이 있습니다.

  • 비트맵을 만들 때 색은 서로 알파 혼합되지 않습니다.

  • 완성된 비트맵을 표시하면 비트맵의 색이 디스플레이 디바이스의 배경색과 알파 혼합됩니다.

이러한 비트맵을 만들려면 빈 Bitmap 개체를 생성한 다음, 해당 비트맵을 기반으로 Graphics 개체를 생성합니다. Graphics 개체의 작성 모드를 CompositingMode.SourceCopy로 설정합니다.

예제

다음 예제에서는 Bitmap 개체를 기반으로 Graphics 개체를 만듭니다. 코드는 두 개의 반투명 브러시(알파 = 160)와 함께 Graphics 개체를 사용하여 비트맵에 그립니다. 이 코드는 반투명 브러시를 사용하여 빨간색 타원과 녹색 타원을 채웁니다. 녹색 타원은 빨간색 타원과 겹치지만 Graphics 개체의 작성 모드가 SourceCopy로 설정되어 있으므로 녹색이 빨간색과 혼합되지 않습니다.

이 코드는 화면에 비트맵을 두 번 그립니다. 한 번은 흰색 배경에, 한 번은 여러 가지 빛깔의 배경에서 그립니다. 두 타원의 일부인 비트맵의 픽셀에는 알파 구성 요소가 160이므로 타원은 화면의 배경색과 혼합됩니다.

다음 그림은 코드 예제의 출력을 보여 줍니다. 타원은 배경과 혼합되지만 서로 혼합되지는 않습니다.

서로가 아닌, 배경과 혼합된 타원을 보여주는 다이어그램.

코드 예제에는 다음 문이 포함되어 있습니다.

bitmapGraphics.CompositingMode = CompositingMode.SourceCopy;
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy

타원을 배경뿐만 아니라 서로 혼합하려면 해당 문을 다음으로 변경합니다.

bitmapGraphics.CompositingMode = CompositingMode.SourceOver;
bitmapGraphics.CompositingMode = CompositingMode.SourceOver

다음 그림은 수정된 코드의 출력을 보여 줍니다.

배경과 함께 혼합된 타원을 보여주는 다이어그램.

// Create a blank bitmap.
Bitmap myBitmap = new Bitmap(180, 100);

// Create a Graphics object that we can use to draw on the bitmap.
Graphics bitmapGraphics = Graphics.FromImage(myBitmap);

// Create a red brush and a green brush, each with an alpha value of 160.
SolidBrush redBrush = new SolidBrush(Color.FromArgb(160, 255, 0, 0));
SolidBrush greenBrush = new SolidBrush(Color.FromArgb(160, 0, 255, 0));

// Set the compositing mode so that when we draw overlapping ellipses,
// the colors of the ellipses are not blended.
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy;

// Fill an ellipse using a red brush that has an alpha value of 160.
bitmapGraphics.FillEllipse(redBrush, 0, 0, 150, 70);

// Fill a second ellipse using a green brush that has an alpha value of 160.
// The green ellipse overlaps the red ellipse, but the green is not
// blended with the red.
bitmapGraphics.FillEllipse(greenBrush, 30, 30, 150, 70);

// Set the compositing quality of the form's Graphics object.
e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected;

// Draw a multicolored background.
SolidBrush colorBrush = new SolidBrush(Color.Aqua);
e.Graphics.FillRectangle(colorBrush, 200, 0, 60, 100);
colorBrush.Color = Color.Yellow;
e.Graphics.FillRectangle(colorBrush, 260, 0, 60, 100);
colorBrush.Color = Color.Fuchsia;
e.Graphics.FillRectangle(colorBrush, 320, 0, 60, 100);

// Display the bitmap on a white background.
e.Graphics.DrawImage(myBitmap, 0, 0);

// Display the bitmap on a multicolored background.
e.Graphics.DrawImage(myBitmap, 200, 0);
' Create a blank bitmap.
Dim myBitmap As New Bitmap(180, 100)

' Create a Graphics object that we can use to draw on the bitmap.
Dim bitmapGraphics As Graphics = Graphics.FromImage(myBitmap)

' Create a red brush and a green brush, each with an alpha value of 160.
Dim redBrush As New SolidBrush(Color.FromArgb(160, 255, 0, 0))
Dim greenBrush As New SolidBrush(Color.FromArgb(160, 0, 255, 0))

' Set the compositing mode so that when we draw overlapping ellipses,
' the colors of the ellipses are not blended.
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy

' Fill an ellipse using a red brush that has an alpha value of 160.
bitmapGraphics.FillEllipse(redBrush, 0, 0, 150, 70)

' Fill a second ellipse using a green brush that has an alpha value of 
' 160. The green ellipse overlaps the red ellipse, but the green is not 
' blended with the red.
bitmapGraphics.FillEllipse(greenBrush, 30, 30, 150, 70)

'Set the compositing quality of the form's Graphics object. 
e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected

' Draw a multicolored background.
Dim colorBrush As New SolidBrush(Color.Aqua)
e.Graphics.FillRectangle(colorBrush, 200, 0, 60, 100)
colorBrush.Color = Color.Yellow
e.Graphics.FillRectangle(colorBrush, 260, 0, 60, 100)
colorBrush.Color = Color.Fuchsia
e.Graphics.FillRectangle(colorBrush, 320, 0, 60, 100)

'Display the bitmap on a white background.
e.Graphics.DrawImage(myBitmap, 0, 0)

' Display the bitmap on a multicolored background.
e.Graphics.DrawImage(myBitmap, 200, 0)

코드 컴파일

앞의 예제는 Windows Forms에서 사용하도록 설계되었으며 PaintEventHandler의 매개 변수인 PaintEventArgse가 필요합니다.

참고 항목