问题描述
大家好,&感谢您阅读本文。
Hi all, & Thanks for reading this.
我已经从流中创建了一个ID2D1Bitmap,但是当我尝试在屏幕上渲染ID2D1Bitmap时,我什么都没看到。变量"pPixels"表示变量"pPixels"。包含正确的数据,所以,直到这一切都是好的。你在我的代码中看到任何问题吗?
I have created a ID2D1Bitmap from a stream, but when I try to render that ID2D1Bitmap on the screen, I see nothing. The variable "pPixels" contain the correct data, so, until this point everything is Ok. Do you see any problem in my code?
非常感谢!
void *pPixels = malloc(nScreenWidth * nScreenHeight * 4);
::GetDIBits(hCaptureDC,
hCaptureBitmap,
0,
nScreenHeight,
pPixels,
&bmi,
DIB_RGB_COLORS);
RECT rect;
GetClientRect(&rect);
D2D1_PIXEL_FORMAT pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED);
UINT32 pitch = nScreenWidth * 4;
D2D1_BITMAP_PROPERTIES d2dBitmapProps =
{
{
DXGI_FORMAT_A8_UNORM,
D2D1_ALPHA_MODE_PREMULTIPLIED
}, 0, 0
};
HRESULT hr;
hr = CreateDeviceResources();
if (!SUCCEEDED(hr)) return;
ASSERT(m_pD2DFactory != NULL && m_pDCRenderTarget != NULL && m_pGreenBrush != NULL);
hr = m_pDCRenderTarget->BindDC(pDC->GetSafeHdc(), &rect);
m_pDCRenderTarget->BeginDraw();
m_pDCRenderTarget->CreateBitmap(D2D1::SizeU(nScreenWidth, nScreenHeight), (const void*)pPixels, pitch, d2dBitmapProps, &pBitmap); // pBitmap - global variable (ID2D1Bitmap* pBitmap = NULL;)
m_pRenderTarget->DrawBitmap(pBitmap, D2D1::RectF(0, 0, nScreenWidth, nScreenHeight));
m_pDCRenderTarget->EndDraw();
这里是"CreateDeviceResources"
And here is "CreateDeviceResources"
HRESULT CDRAWIXView::CreateDeviceResources()
{
HRESULT hr = S_OK;
if (m_pD2DFactory == NULL) return E_FAIL;
if (m_pDCRenderTarget == NULL)
{
D2D1_RENDER_TARGET_PROPERTIES props =
D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(
DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_PREMULTIPLIED
), 0.0F, 0.0F,
D2D1_RENDER_TARGET_USAGE_GDI_COMPATIBLE
);
hr = m_pD2DFactory->CreateDCRenderTarget(
&props, &m_pDCRenderTarget);
if (!SUCCEEDED(hr)) return hr;
}
return hr;
}
推荐答案
感谢您在此发布。
>> I从流中创建了一个ID2D1Bitmap,但是当我尝试在屏幕上渲染ID2D1Bitmap时,我什么都没看到。变量"pPixels"表示变量"pPixels"。包含正确的数据,所以,直到这一切都是好的。你看到我的代码在
中有什么问题吗?
如果DrawBitmap方法失败,它不会返回错误代码。判断是否有绘图操作 失败,检查
返回的结果或方法。
The DrawBitmap method doesn't return an error code if it fails. To determine whether a drawing operation failed, check the result returned by theID2D1RenderTarget::EndDraw or ID2D1RenderTarget::Flush methods.
然后提供错误代码(如果有),以便我们找到根本原因。
Then provide the error code if there has, so that we could find the root cause.
最好的问候,
Sera Yu
这篇关于ID2D1Bitmap / DrawBitmap不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!