wxWidgets绘制透明度问题

时间:2016-05-20 21:58:40

标签: bitmap wxwidgets

以下是相关的代码行。基本上我所做的是创建一个更大的位图,绘制到这个,然后从中创建一个较小的图像并将其绘制到屏幕(wxDC mdc)。为了使它透明,我首先使用wxMemoryDC和wxGCDC,因为这是我弄清楚如何的唯一方法。

问题是,除非剪掉的sub_bmp没有绘制任何东西,否则它的效果非常好,然后它只绘制黑色背景而不是透明。

有什么想法吗?

*bmp = wxBitmap(bwidth, bheight, 32);       
bmp->UseAlpha();
wxColor colour;
colour.Set("#800020");
penWidth = 4;
mdc->SetPen(wxPen(colour, penWidth));
wxMemoryDC memDC (*bmp);
wxGCDC dc(memDC);
dc.SetBackground(*wxTRANSPARENT_BRUSH);
dc.Clear();

dc.SetBrush(*wxRED_BRUSH);
dc.SetPen(wxPen(colour, penWidth));
...
b1.x = pix_offset_x - (cpix.x - b1.x);                  
b1.y = pix_offset_y - (cpix.y - b1.y);
b2.x = pix_offset_x - (cpix.x - b2.x);
b2.y = pix_offset_y - (cpix.y - b2.y);

dc.DrawLine(b1, b2);
memDC.SelectObject(wxNullBitmap);           //releases the bitmap from memDC
wxRect subSize(xloc,yloc , vp->pix_width*scaleFactor, vp->pix_height*scaleFactor);
wxBitmap sub_bmp = bmp->GetSubBitmap(subSize);
wxImage tmpimg = sub_bmp.ConvertToImage();
const wxBitmap tbmp(tmpimg.Scale(t_width, t_height),32);
mdc->DrawBitmap(tbmp, 0, 0, true);

1 个答案:

答案 0 :(得分:0)

假设您使用的是最新版本的wxWidgets,您可以直接绘制到wxImage,但首先必须设置alpha通道。

然后可以缩放图像并将其复制到当前正在执行的mdc中。

// Setup the alpha channel.
unsigned char* alphaData = new unsigned char[bwidth * bheight];
memset (alphaData, wxIMAGE_ALPHA_TRANSPARENT, bwidth * bheight);

// Create an image with alpha.
wxImage image (wxSize(bwidth, bheight));
image.SetAlpha (alphaData);
wxGraphicsContext* gc = wxGraphicsContext::Create (image);

gc->SetPen (wxPen(colour, penWidth));
gc->SetBrush (wxTRANSPARENT_BRUSH);

// Do drawing here ....

// Release the graphics context.
delete gc;

// Scale the image and convert to a bitmap.
wxBitmap outBmp (image.Scale(t_width, t_height), 32);

// Blit it to the provide mdc.
mdc->DrawBitmap (outBmp, wxPoint(x,y)), true);