GDI rendering to direct2D ID2D1BitmapRenderTarget is always transparent

五迷三道 提交于 2019-12-05 16:20:11

I had the same problem. The following worked for me (you need to create the render target using the D2D1_ALPHA_MODE_IGNORE, not D2D1_ALPHA_MODE_PREMULTIPLIED).

ID2D1HwndRenderTarget* pRenderTarget; // render target created sometime earlier

D2D1_PIXEL_FORMAT pixelFormat = D2D1::PixelFormat(
    DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE);

ID2D1BitmapRenderTarget* pOffscreenRT = NULL;
pRenderTarget->CreateCompatibleRenderTarget(NULL, NULL, &pixelFormat,
    D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_GDI_COMPATIBLE, &pOffscreenRT);

There's a dirty little secret about rendering with GDI: It will always clobber the alpha channel. Anything you draw with it will set the alpha values to zero in that area. I suspect it's because it was never designed to work with an alpha channel or any type of compositing. It was designed to render directly to the screen and to printers, where alpha channels don't exist.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!