c++ – I want to create an empty bitmap in directx, draw a picture and then output it
The result of all function calls is S_OK.
I was expecting a blue square to appear on the screen.
But it’s just a black screen.
I am newbie to directx.
I’m not sure what the problem is. T_T
Please let me know if I’m misunderstanding or missing a procedure..
this is my code.
draw.cpp
Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> pBlackBrush;
m_deviceContext->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::Blue),
&pBlackBrush
);
D2D1_BITMAP_PROPERTIES newBitmapProperties =
D2D1::BitmapProperties(
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),
96,
96
);
m_deviceContext->SetTarget(m_d2dTargetBitmap.Get());
Microsoft::WRL::ComPtr<IWICBitmap> newWicBitmap;
auto ret3 = m_wicFactory->CreateBitmap(300, 300, GUID_WICPixelFormat32bppPRGBA, WICBitmapCacheOnDemand, newWicBitmap.GetAddressOf());
D2D1_RENDER_TARGET_PROPERTIES m_RenderTargetProp = D2D1::RenderTargetProperties();
m_RenderTargetProp.dpiX = 96;
m_RenderTargetProp.dpiY = 96;
m_RenderTargetProp.minLevel = D2D1_FEATURE_LEVEL_DEFAULT;
m_RenderTargetProp.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED);
m_RenderTargetProp.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
m_RenderTargetProp.usage = D2D1_RENDER_TARGET_USAGE_NONE;
Microsoft::WRL::ComPtr<ID2D1RenderTarget> bitmapRender;
auto ret2 = pD2DFactory->CreateWicBitmapRenderTarget(newWicBitmap.Get(), m_RenderTargetProp, bitmapRender.GetAddressOf());
if (ret2 != S_OK)
OutputDebugString(L"BitmapRenderTarget Error\n");
bitmapRender->BeginDraw();
D2D1_RECT_F f{ 0,0,200,200 };
bitmapRender->FillRectangle(D2D1::RectF(
0,
0,
100.0f,
100.0f), pBlackBrush.Get());
bitmapRender->EndDraw();
Microsoft::WRL::ComPtr<ID2D1Bitmap> newBitmap;
ret3 = bitmapRender->CreateBitmapFromWicBitmap(newWicBitmap.Get(), newBitmap.GetAddressOf());
m_deviceContext->BeginDraw();
m_deviceContext->Clear(D2D1::ColorF(D2D1::ColorF::Black));
m_deviceContext->DrawBitmap(newBitmap.Get());
m_deviceContext->EndDraw();
DXGI_PRESENT_PARAMETERS parameters{ 0, };
m_swapChain->Present1(1, 0, ¶meters);
Read more here: Source link