簡體   English   中英

如何在Direct2D中創建透明位圖

[英]How to create a transparent bitmap in 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 ,但沒有成功。

有什么解決方案嗎?

好消息Nick:您正在創建透明位圖,但是您沒有看到預期結果的原因與窗口創建有關。 Kenny Kerr 在2009年展示了使用Direct2D創建適當的分層窗口。

從那以后,在Windows渲染方面發生了很多變化,Win8和Win10使用了合成引擎,並允許開發人員訪問DirectComposition API。 因此,Kenny Kerr 在2014年就該主題提供了一篇更新的文章

我最近的項目需要Win7支持,所以我個人堅持使用純Direct2D。

編輯:當然,確保正確創建渲染目標 希望這可以幫助。

當我需要創建緩存渲染(位圖)時,我在Sciter中執行的操作如下

ID2D1RenderTarget* src = ...
d2d::asset<ID2D1BitmapRenderTarget> dst = nullptr;

// creating temp surface - compatible render target:
src->CreateCompatibleRenderTarget(sz,dst.target());

dst->BeginDraw();
dst->Clear(init_color);

... drawing on that temp surface ...
dst->EndDraw();

d2d::asset<ID2D1Bitmap> dst_bmp;
hr = dst->GetBitmap(il->d2d_bmp.target());

return dst_bmp;

此方法將位圖創建委托給ID2D1RenderTarget,以便位圖始終與源兼容。 該代碼主要用於透明的init_color。

我不會使用SetTarget()因為它不清楚它對剪輯等的作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM