흐르는 시간의 블로그...

알파 블랜드로 한번 해보자 ㅠ.ㅠ

------------------------------------------------------------------------------------------

GDI 에선 AlphaBlend 라는 함수로 가능하기는 하는데

찍을 부분의 이미지 색상과 혼합을 해야 제대로 구현이 됩니다

계산식이야 MSDN 에도 있고 여기 Q&A 에 검색해도 있습니다.

AlphaBlend 로 하다가 계산하는거 짜증나서

GDI+ 찾아 보니까 있더군여..

MSDN 에서 발췌했습니다.

여기 검색해봐도 없길래... 갖다 붙여 넣습니다... 참고하세요..


Using a Color Matrix to Set Alpha Values in Images

The Bitmap class (which inherits from the Image class) and the ImageAttributes class provide functionality for getting and setting pixel values. You can use the ImageAttributes class to modify the alpha values for an entire image, or you can call the SetPixel method of the Bitmap class to modify individual pixel values. For more information on setting individual pixel values, see Setting the Alpha Values of Individual Pixels.

The following example draws a wide black line and then displays an opaque image that covers part of that line.

Bitmap bitmap(L"Texture1.jpg");
Pen pen(Color(255, 0, 0, 0), 25);

// First draw a wide black line.
graphics.DrawLine(&pen, Point(10, 35), Point(200, 35));

// Now draw an image that covers part of the black line.
graphics.DrawImage(&bitmap,
Rect(30, 0, bitmap.GetWidth(), bitmap.GetHeight()));

The following illustration shows the resulting image, which is drawn at (30, 0). Note that the wide black line doesn't show through the image.

The ImageAtributes class has many properties that you can use to modify images during rendering. In the following example, an ImageAttributes object is used to set all the alpha values to 80 percent of what they were. This is done by initializing a color matrix and setting the alpha scaling value in the matrix to 0.8. The address of the color matrix is passed to the SetColorMatrix method of the ImageAttributes object, and the address of the ImageAttributes object is passed to the DrawImage method of a Graphics object.

// 다음 코드가 반투명 효과를 일으키게하는 소스이며.. 주석과 같이 4행4열의 0.8값을 주의깊게 보십시요

// 이 값을 바꿔주면서 이미지를 확인해 보시면 잘 알수 있을겁니다...

// 그럼.. 참고하세요..

// Create a Bitmap object and load it with the texture image.
Bitmap bitmap(L"Texture1.jpg");
Pen pen(Color(255, 0, 0, 0), 25);

// Initialize the color matrix.
// Notice the value 0.8 in row 4, column 4.
ColorMatrix colorMatrix = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.8f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f};

// Create an ImageAttributes object and set its color matrix.
ImageAttributes imageAtt;
imageAtt.SetColorMatrix(&colorMatrix, ColorMatrixFlagsDefault,
ColorAdjustTypeBitmap);

// First draw a wide black line.
graphics.DrawLine(&pen, Point(10, 35), Point(200, 35));

// Now draw the semitransparent bitmap image.
INT iWidth = bitmap.GetWidth();
INT iHeight = bitmap.GetHeight();
graphics.DrawImage(
&bitmap,
Rect(30, 0, iWidth, iHeight), // Destination rectangle
0, // Source rectangle X
0, // Source rectangle Y
iWidth, // Source rectangle width
iHeight, // Source rectangle height
UnitPixel,
&imageAtt);

During rendering, the alpha values in the bitmap are converted to 80 percent of what they were. This results in an image that is blended with the background. As the following illustration shows, the bitmap image looks transparent; you can see the solid black line through it.

Where the image is over the white portion of the background, the image has been blended with the color white. Where the image crosses the black line, the image is blended with the color black.