다음을 통해 공유


작성 모드를 사용하여 알파 혼합 제어

다음과 같은 특징이 있는 화면 외부 비트맵을 만들려는 경우가 있을 수 있습니다.

  • 색에는 255보다 작은 알파 값이 있습니다.
  • 비트맵을 만들 때 색은 서로 알파 혼합되지 않습니다.
  • 완성된 비트맵을 표시하면 비트맵의 색이 디스플레이 디바이스의 배경색과 알파 혼합됩니다.

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

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

다음으로 코드는 BeginPaint 를 호출하고 디바이스 컨텍스트를 기반으로 Graphics 개체를 만들어 화면에 그릴 준비를 합니다. 이 코드는 화면에 비트맵을 두 번 그립니다. 한 번은 흰색 배경에, 한 번은 여러 가지 빛깔의 배경에서 그립니다. 두 타원의 일부인 비트맵의 픽셀에는 알파 구성 요소가 160이므로 타원은 화면의 배경색과 혼합됩니다.

// Create a blank bitmap.
Bitmap bitmap(180, 100);
// Create a Graphics object that can be used to draw on the bitmap.
Graphics bitmapGraphics(&bitmap);
// Create a red brush and a green brush, each with an alpha value of 160.
SolidBrush redBrush(Color(210, 255, 0, 0));
SolidBrush greenBrush(Color(210, 0, 255, 0));
// Set the compositing mode so that when overlapping ellipses are drawn,
// the colors of the ellipses are not blended.
bitmapGraphics.SetCompositingMode(CompositingModeSourceCopy);
// 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 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);
// Prepare to draw on the screen.
hdc = BeginPaint(hWnd, &ps);
Graphics* pGraphics = new Graphics(hdc);
pGraphics->SetCompositingQuality(CompositingQualityGammaCorrected);
// Draw a multicolored background.
SolidBrush brush(Color((ARGB)Color::Aqua));
pGraphics->FillRectangle(&brush, 200, 0, 60, 100);
brush.SetColor(Color((ARGB)Color::Yellow));
pGraphics->FillRectangle(&brush, 260, 0, 60, 100);
brush.SetColor(Color((ARGB)Color::Fuchsia));
pGraphics->FillRectangle(&brush, 320, 0, 60, 100);
   
// Display the bitmap on a white background.
pGraphics->DrawImage(&bitmap, 0, 0);
// Display the bitmap on a multicolored background.
pGraphics->DrawImage(&bitmap, 200, 0);
delete pGraphics;
EndPaint(hWnd, &ps);

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

서로 다른 색의 타원 두 개를 보여 주는 일러스트레이션. 각 타원은 여러 가지 색의 배경과 혼합됩니다.

앞의 코드 예제에는 다음 문이 있습니다.

bitmapGraphics.SetCompositingMode(CompositingModeSourceCopy);

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

bitmapGraphics.SetCompositingMode(CompositingModeSourceOver);

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

작성 모드를 사용하여 알파 혼합 일러스트레이션 제어