![](/img/trans.png)
[英]Direct2D refuses to draw a bitmap onto a window, fails silently
[英]How to draw a image with effects that rescales with it's window in direct2d
我按照這個例子創建了一個 Direct2D 應用程序: https://github.com/microsoft/Windows-classic-samples/tree/master/Samples/Win7Samples/multimedia/Direct2D/SimpleDirect2DApplication
我設法在應用程序 window 中顯示了 bitmap 和 bitmap 在重新縮放 Z05B8C7352706FBF2FZ4C1 時也會重新縮放。 但現在我想在 bitmap 上應用效果,我的問題來了。 效果是這樣應用的
hr = m_pRenderTarget->QueryInterface( __uuidof(ID2D1DeviceContext), (void**)&m_pDeviceContext );
m_pDeviceContext->CreateEffect( CLSID_D2D1GammaTransfer, &gammaTransferEffect );
gammaTransferEffect->SetInput( 0, m_pBitmap );
gammaTransferEffect->SetValue( D2D1_GAMMATRANSFER_PROP_RED_AMPLITUDE, 4.0f );
問題是應用效果后,圖像數據現在是ID2D1Effect格式。 這可以像這樣用DrawImage繪制:
m_pDeviceContext->DrawImage(gammaTransferEffect);
但是我在 function DrawBitmap中使用destinationRectangle
進行了重新縮放,並且在DrawImage
中沒有與destinationRectangle
等效。
m_pDeviceContext->DrawBitmap(
m_pBitmap,
D2D1::RectF(
0,
0,
renderTargetSize.width,
renderTargetSize.height),
1.0f,
D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR
);
那么,在重新縮放渲染目標時應用效果后,如何重新縮放 bitmap? 我對此有一些想法,但沒有一個讓我找到解決方案。
有人知道這里有什么解決方案嗎?
我的問題的一種解決方案是重新縮放渲染目標:
// Retrieve the size of the render target.
D2D1_SIZE_F renderTargetSize = m_pRenderTarget->GetSize();
// calculate scale factors
float scale_x = renderTargetSize.width / _bitmapSource.width;
float scale_y = renderTargetSize.height / _bitmapSource.height;
// scale render target
m_pRenderTarget->SetTransform(
D2D1::Matrix3x2F::Scale(
D2D1::Size( scale_x, scale_y ),
D2D1::Point2F( 0.0f, 0.0f ))
);
hr = m_pRenderTarget->QueryInterface( __uuidof(ID2D1DeviceContext), (void**)&m_pDeviceContext );
m_pDeviceContext->CreateEffect( CLSID_D2D1GammaTransfer, &gammaTransferEffect );
gammaTransferEffect->SetInput( 0, m_pBitmap );
gammaTransferEffect->SetValue( D2D1_GAMMATRANSFER_PROP_RED_AMPLITUDE, 4.0f );
m_pDeviceContext->DrawImage(gammaTransferEffect);
此外,我必須從WM_SIZE
消息中刪除我的resize
function,否則我繪制的 bitmap 沒有用它的 window 重新縮放。 這對我來說有點違反直覺,但它確實有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.