WxWidgets draw a bitmap with opacity

gdi+wxwidgets

How can I draw a wxImage, or wxBitmap to a DC with opacity? It looks like its not possible with the standard DC or with wxGraphicsContext.

BeginLayer/EndLayer is not yet implemented in wxGraphicsContext which would have been one solution. GDI+ doesn't seem to support layers, so it would be non-trivial to add this.

The only solution seems to be to programmatically alter the alpha channel pixel-by-pixel?

Best Answer

void YourCustomWindow::OnPaint( wxPaintEvent& event ) {
  wxPaintDC dc(this);
  wxImage image( width, height ); 
  image.InitAlpha();
  unsigned char* imgdata = img.GetData();
  unsigned char* imgalpha = img.GetAlpha();


  // manipulate raw memory buffers to populate
  // them with whatever image you like.
  // Putting zeros everywhere will create a fully
  // transparent image.


     // almost done, but not quite... 
     // wxDC can only draw wxBitmaps, not wxImage,
     // so one last step is required
  //
  wxBitmap bmp( image );
  dc.DrawBitmap( bmp, 0, 0 );
 }
Related Topic