![](/img/trans.png)
[英]How to draw a image with effects that rescales with it's window in direct2d
[英]Direct2D refuses to draw a bitmap onto a window, fails silently
我正在嘗試使用 Direct2D 將 128x128 bitmap 繪制到 window 上。 但是,什么也沒有顯示, EndDraw()
也沒有返回任何錯誤。
這是我的WM_PAINT
代碼:
case WM_PAINT:
D2D1_RECT_F testd2dbuttonrect;
mainwRT->BeginDraw();
mainwRT->SetTransform( D2D1::Matrix3x2F::Identity() );
testd2dbutton.pd2drectgm->GetRect( &testd2dbuttonrect );
mainwRT->FillRectangle( &testd2dbuttonrect, pSolidBrush );
//This is where I'm trying to draw my bitmap
mainwRT->FillRectangle( D2D1::RectF(0.0f,0.0f,127.0f,127.0f), pBgndBrush );
errmsg = mainwRT->EndDraw();
if( !SUCCEEDED(errmsg) )
printf("EndDraw() error: %d\r\n", errmsg );
break;
我懷疑這可能是因為我從 Visual Studio 資源加載的 bitmap 數據是垃圾,但我無法知道,因為沒有任何失敗並給出錯誤消息。 這是我用來從資源加載ID2D1Bitmap
的代碼:
int LoadBitmapFromResource( IWICImagingFactory *pImageFactory, ID2D1RenderTarget *pRT, int resID, ID2D1Bitmap **ppD2DBitmap )
{
int errmsg;
HBITMAP hbitmap;
WICBitmapAlphaChannelOption wicalpha;
IWICBitmap *pwicbitmap;
IWICBitmapSource *pconvertedwicbitmap;
IWICFormatConverter *pConverter;
ID2D1Factory *d2dfactory;
D2D1_BITMAP_PROPERTIES d2dbp;
D2D1_PIXEL_FORMAT d2dpf;
FLOAT dpiX;
FLOAT dpiY;
hbitmap = LoadBitmap( GetModuleHandle(NULL), MAKEINTRESOURCEW(resID) );
wicalpha = WICBitmapUseAlpha;
errmsg = pImageFactory->CreateBitmapFromHBITMAP( hbitmap, NULL, wicalpha, &pwicbitmap );
if( !SUCCEEDED(errmsg) )
{
printf("LoadBitmapFromResource::CreateBitmapFromHBITMAP() error: %x\r\n", errmsg );
return errmsg;
}
errmsg = pImageFactory->CreateFormatConverter( &pConverter );
if( !SUCCEEDED(errmsg) )
{
printf("LoadBitmapFromResource::CreateFormatConverter() error: %x\r\n", errmsg );
return errmsg;
}
d2dpf.format = DXGI_FORMAT_B8G8R8A8_UNORM;
d2dpf.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
pRT->GetFactory( &d2dfactory );
d2dfactory->GetDesktopDpi( &dpiX, &dpiY );
d2dbp.pixelFormat = d2dpf;
d2dbp.dpiX = dpiX;
d2dbp.dpiY = dpiY;
pConverter->Initialize( pwicbitmap, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.0f, WICBitmapPaletteTypeMedianCut );
if( !SUCCEEDED(errmsg) )
{
printf("LoadBitmapFromResource::Initialize() error: %x\r\n", errmsg );
return errmsg;
}
errmsg = WICConvertBitmapSource( GUID_WICPixelFormat32bppPBGRA, pwicbitmap, &pconvertedwicbitmap );
if( !SUCCEEDED(errmsg) )
{
printf("LoadBitmapFromResource::WICConvertBitmapSource() error: %x\r\n", errmsg );
return errmsg;
}
errmsg = pRT->CreateBitmapFromWicBitmap( pconvertedwicbitmap, &d2dbp, ppD2DBitmap );
if( !SUCCEEDED(errmsg) )
{
printf("LoadBitmapFromResource::CreateBitmapFromWicBitmap() error: %x\r\n", errmsg );
return errmsg;
}
pConverter->Release();
pwicbitmap->Release();
DeleteObject( hbitmap );
return 0;
}
我在 Visual Studio 2010 中使用 C++ 和本機 WinAPI。
我認為您仍然必須在 WM_PAINT 中使用經典的 BeginPaint(),然后進行 D2D 渲染,然后調用 EndPaint()。 這就是我的代碼所做的,我認為這也是 MSDN 示例代碼 gizmo 所做的。
從資源加載 bitmap 時我遇到了同樣的問題,而 D2D 在運行 CreateBitmapFromWicBitmap 時遇到了問題。 在我的同事的幫助下,關鍵問題是使用 WICBitmapIgnoreAlpha,因為 Bitmap 沒有 Alpha(至少我的資源中的 BITMAP 沒有 Alpha)。
甚至你不需要轉換器,只需要簡單的代碼
hbitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(resID) );
if (hbitmap==0)
{
SafeRelease(&pImageFactory);
return nullptr;
}
hr = pImageFactory->CreateBitmapFromHBITMAP( hbitmap, NULL, wicalpha, &pwicbitmap );
if( !SUCCEEDED(hr) )
{
DeleteObject( hbitmap );
SafeRelease(&pImageFactory);
return nullptr;
}
ID2D1Bitmap* ppD2DBitmap=nullptr;
hr = pRT->CreateBitmapFromWicBitmap( pwicbitmap, &ppD2DBitmap );
調試它的方法是使用各種 API 鎖定調用來獲取指向圖像數據的指針,然后通過調試器或 printf 檢查圖像數據,以驗證數據是否符合您的預期。
HBITMAP 和 D2D 表面有不同的 Lock 調用,但可以使用相同的想法。 您應該能夠縮小獲取不良數據的范圍。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.