How to generate a .png file with a transparent background?

  1. FAQ
  2. »
  3. Raster Image Questions

Set the bitmap device to 32 bits per pixel, as at the default 24, the image has no alpha channel, and use a background colour with an alpha value below 255:

pDevice->properties()->putAt(OD_T("BitPerPixel"), OdRxVariantValue(OdUInt32(32)));
pDevice->setBackgroundColor(ODRGBA(255, 255, 255, 0));

Then save the RasterImage device property to a .png file using RxRasterServices. 

For a complete example, see Drawing/Examples/SimpleExportToRaster. There, add the BitPerPixel line and set bgColor to ODRGBA(255, 255, 255, 0) before pDevice->update().