How to Draw a BitmapSource Using Direct2D
This topic demonstrates the process for drawing an IWICBitmapSource by using Direct2D.
To draw a bitmap source by using Direct2D
Decode a source image and obtain a bitmap source. In this example, an IWICBitmapDecoder is used to decode the image and the first image frame is retrieved.
// 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); }
For additional types of bitmap sources to draw, see the Bitmap Sources Overview.
Convert the bitmap source to an 32bppPBGRA pixel format.
Before Direct2D can use an image, it must be converted to the 32bppPBGRA pixel format. To convert the image format, use the CreateFormatConverter method to create an IWICFormatConverter object. Once created, use the Initialize method to perform the 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 ); }
Create an ID2D1RenderTarget object for rendering to a window handle.
// 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 );
For more information render targets, see the Direct2D Render Targets Overview.
Create an ID2D1Bitmap object using the ID2D1RenderTarget::CreateBitmapFromWicBitmap method.
// 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); }
Draw the ID2D1Bitmap using D2D ID2D1RenderTarget::DrawBitmap method.
// Draws an image and scales it to the current window size if (m_pD2DBitmap) { m_pRT->DrawBitmap(m_pD2DBitmap, rectangle); }
Code has been omitted from this example. For the complete code, see the WIC Image Viewer Using Direct2D Sample.
See Also