如何绘制带有透明背景(GDI)的patternBrush?

问题描述:

我无法绘制具有透明背景的图案.这是我的代码段:

I can't draw a pattern with a transparent background. This is my snippet :

bitmap.CreateBitmap(8, 8, 1, 1, &bits)
brush.CreatePatternBrush(&bitmap)
hbrush = pCgrCurrentDC->SelectObject(&brush);
// set text color 
TextCol = pCgrCurrentDC->SetTextColor(CgrColourPalRGB);  
int oldBkgrdMode = pCgrCurrentDC->SetBkMode(TRANSPARENT);
//draw polygon 
pCgrCurrentDC->Polygon(CgrBuffer, n);

msdn 不会"什么都没有提到透明度.我想可以使用这种模式吗?还是这是一个错误?

The doc on msdn doesn't mention anything about transparency. I guess this mode could be used? Or is this a bug ?

谢谢!

模式TRANSPARENT表示在绘制画笔之前不会填充背景.但是您的笔刷中不包含任何透明像素,并且无论如何它都会重绘背景像素.在示例中,CreateBitmap中的第四个参数设置为1.这意味着位图是单色的.

Mode TRANSPARENT means that background will not be filled before your brush is drawn. But your brush does not contain any transparent pixels in it and it redraws background pixels anyway. Fourth argument in CreateBitmap was set to 1 in your sample. That means bitmap is monochrome.

您需要使用32位位图才能在笔刷中使用透明度. GDI支持透明性,但有一些限制.使用GDI +可获得完全透明的支持.

You need to use 32-bit bitmap to use transparency in brushes. GDI supports transparency with some limits. Use GDI+ for full transparency support.