Comment dessiner une bitmapSource à l’aide de Direct2D
Cette rubrique illustre le processus de dessin d’un IWICBitmapSource à l’aide de Direct2D.
Pour dessiner une source bitmap à l’aide de Direct2D
Décodez une image source et obtenez une source bitmap. Dans cet exemple, un IWICBitmapDecoder est utilisé pour décoder l’image et le premier frame d’image est récupéré.
// Create a decoder IWICBitmapDecoder *pDecoder = NULL; hr = m_pIWICFactory->CreateDecoderFromFilename( szFileName, // Image to be decoded NULL, // Do not prefer a particular vendor GENERIC_READ, // Desired read access to the file WICDecodeMetadataCacheOnDemand, // Cache metadata when needed &pDecoder // Pointer to the decoder ); // Retrieve the first frame of the image from the decoder IWICBitmapFrameDecode *pFrame = NULL; if (SUCCEEDED(hr)) { hr = pDecoder->GetFrame(0, &pFrame); }
Pour d’autres types de sources bitmap à dessiner, consultez vue d’ensemble des sources bitmap.
Convertissez la source bitmap au format de pixel 32bppPBGRA.
Avant que Direct2D puisse utiliser une image, elle doit être convertie au format de pixel 32bppPBGRA. Pour convertir le format d’image, utilisez la méthode CreateFormatConverter pour créer un objet IWICFormatConverter . Une fois créée, utilisez la méthode Initialize pour effectuer la conversion.
// Format convert the frame to 32bppPBGRA if (SUCCEEDED(hr)) { SafeRelease(&m_pConvertedSourceBitmap); hr = m_pIWICFactory->CreateFormatConverter(&m_pConvertedSourceBitmap); } if (SUCCEEDED(hr)) { hr = m_pConvertedSourceBitmap->Initialize( pFrame, // Input bitmap to convert GUID_WICPixelFormat32bppPBGRA, // Destination pixel format WICBitmapDitherTypeNone, // Specified dither pattern NULL, // Specify a particular palette 0.f, // Alpha threshold WICBitmapPaletteTypeCustom // Palette translation type ); }
Créez un objet ID2D1RenderTarget pour le rendu sur un handle de fenêtre.
// Create a D2D render target properties D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties = D2D1::RenderTargetProperties(); // Set the DPI to be the default system DPI to allow direct mapping // between image pixels and desktop pixels in different system DPI settings renderTargetProperties.dpiX = DEFAULT_DPI; renderTargetProperties.dpiY = DEFAULT_DPI; // Create a D2D render target D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top); hr = m_pD2DFactory->CreateHwndRenderTarget( renderTargetProperties, D2D1::HwndRenderTargetProperties(hWnd, size), &m_pRT );
Pour plus d’informations sur les cibles de rendu, consultez vue d’ensemble des cibles de rendu Direct2D.
Créez un objet ID2D1Bitmap à l’aide de la méthode ID2D1RenderTarget::CreateBitmapFromWicBitmap .
// D2DBitmap may have been released due to device loss. // If so, re-create it from the source bitmap if (m_pConvertedSourceBitmap && !m_pD2DBitmap) { m_pRT->CreateBitmapFromWicBitmap(m_pConvertedSourceBitmap, NULL, &m_pD2DBitmap); }
Dessinez l’ID2D1Bitmap à l’aide de la méthode D2D ID2D1RenderTarget::D rawBitmap .
// Draws an image and scales it to the current window size if (m_pD2DBitmap) { m_pRT->DrawBitmap(m_pD2DBitmap, rectangle); }
Le code a été omis dans cet exemple. Pour obtenir le code complet, consultez l’exemple WiC Image Viewer Using Direct2D.
Voir aussi