user1489223
user1489223

Reputation: 153

wxWidgets drawing transparency issue

Below is the relevant lines of code. Essentially what I do is create a larger bitmap, draw to this, and then create a smaller image from that and draw it to the screen (wxDC mdc). In order to get it transparent I first use a wxMemoryDC and wxGCDC since this is the only way i could figure out how.

The issue is that, it works perfectly unless the clipped out sub_bmp has nothing drawn to it, then it just draws a black background instead of transparent.

Any ideas?

*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);

Upvotes: 1

Views: 1645

Answers (1)

iwbnwif
iwbnwif

Reputation: 327

Assuming that you are using a recent version of wxWidgets, you can draw directly to the wxImage but first you must setup the alpha channel.

Then the image can be scaled and copied to the provided mdc as you are doing currently.

// 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);

Upvotes: 1

Related Questions