【问题标题】:Store ID2D1Effect output in ID2D1Bitmap/Image to draw it at a later time?将 ID2D1Effect 输出存储在 ID2D1Bitmap/Image 中以便以后绘制?
【发布时间】:2025-12-08 09:55:02
【问题描述】:

我想将 ID2D1Effect 的输出图像转换为 ID2D1Bitmap,这样我就可以在以后绘制它,而无需一遍又一遍地应用所有效果...

我的第一次尝试只是保留 ID2D1Effect::GetOutput 图像 ptr,但如果我将效果与另一个图像源一起使用,此图像会发生变化...

我的下一个尝试是创建一个设置了 D2D1_BITMAP_OPTIONS_TARGET 标志的位图 (ID2D1DeviceContext::CreateBitmap) 并将效果输出绘制到该位图,但这似乎也不起作用...

绘制效果到给定的 Bitmap1** (dest):

ComPtr<ID2D1Image> swapChainImageBuffer;
    this->pDeviceContext->CreateBitmap(D2D1::SizeU(120, 50), nullptr, 0, D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET), dest);
    this->pDeviceContext->GetTarget(swapChainImageBuffer.GetAddressOf());
    this->pDeviceContext->SetTarget(*dest);
    this->pDeviceContext->BeginDraw();
    this->pDeviceContext->Clear(D2D1::ColorF(RGB(0, 0, 0), 1.f));
    this->pDeviceContext->DrawImage(this->pCompositeEffect.Get(), D2D1::Point2F(20, 10));    
    this->pDeviceContext->EndDraw();
    this->pDeviceContext->SetTarget(swapChainImageBuffer.Get());
    swapChainImageBuffer = nullptr;

稍后绘制位图:(预览)

this->pD2DeviceContext->DrawBitmap(preview.Get(), D2D1::RectF(x, y, x+width, y + height));

(相同的 DeviceContext - 没有返回 HRESULT 的函数返回错误代码)

我做错了什么?

我该怎么做?

【问题讨论】:

  • 要查找问题,启用 Direct2D(和 Direct3D)的调试层很有用。

标签: c++ effect direct2d


【解决方案1】:

只是忘记在 CreateBitmap 中指定 PixelFormat。

D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)

现在可以使用了。

【讨论】:

    最近更新 更多