如何在Direct2D中创建透明位图 [英] How to create a transparent bitmap in Direct2D

查看:168
本文介绍了如何在Direct2D中创建透明位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Direct2D创建透明的位图,并使用设备上下文在其上绘制.

  ID2D1DeviceContext1 * d2dContext = ...ID2D1Bitmap * pBitmap;d2dContext-> CreateBitmap(bitmapSize,nullptr,0,D2D1 :: BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET,D2D1 :: PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,D2D1_ALPHA_MODE_PREMULTIPLIED),dpiX,dpiY),& pBitmap);d2dContext-> BeginDraw();d2dContext-> SetTarget(pBitmap);d2dContext-> Clear(D2D1 :: ColorF(0,0));d2dContext-> DrawLine(...);hr = d2dContext-> EndDraw(); 

不幸的是,我无法创建任何透明的位图.我尝试了几种像素格式组合,包括 D2D1_ALPHA_MODE_STRAIGHT ,但没有成功.

有什么解决办法吗?

解决方案

好消息尼克:您正在创建透明位图,但是未看到预期结果的原因与窗口创建有关.肯尼·科尔(Kenny Kerr)早在2009年就在这里使用Direct2D演示了正确的分层窗口创建.

从那时起,Windows渲染发生了很大变化,Win8和Win10使用了合成引擎,并允许开发人员访问DirectComposition API.结果,肯尼·科尔(Kenny Kerr)提供了 2014年有关该主题的更新文章.

>

我最近的项目需要Win7支持,所以我个人坚持使用纯Direct2D.

编辑:当然还要确保您的pixel format combinations, including D2D1_ALPHA_MODE_STRAIGHT, but without succeeding.

Is there any solution?

解决方案

Good news Nick: You are creating transparent bitmaps, however the reason why you are not seeing your expected results has to do with window creation. Kenny Kerr demonstrated proper layered window creation with Direct2D back in 2009 here.

Since then much has changed with respect to windows rendering, with Win8 and Win10 using a composition engine, and allowing developers access to the DirectComposition API. As a result, Kenny Kerr provided an updated article in 2014 on the topic.

My recent projects required Win7 support, so I personally stick with pure Direct2D.

EDIT: And of course ensure your render target is properly created. Hope this helps.

这篇关于如何在Direct2D中创建透明位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆