Come disegnare un oggetto BitmapSource con Direct2D
In questo argomento viene illustrato il processo di disegno di un IWICBitmapSource tramite Direct2D.
Per disegnare un'origine bitmap tramite Direct2D
Decodificare un'immagine di origine e ottenere un'origine bitmap. In questo esempio viene usato unIWICBitmapDecoderper decodificare l'immagine e viene recuperato il primo frame di immagine.
// 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); }
Per ulteriori tipi di origini bitmap da disegnare, vedere la Panoramica delle Origini Bitmap .
Convertire la sorgente bitmap in un formato di pixel 32bppPBGRA.
Prima che Direct2D possa usare un'immagine, deve essere convertita nel formato pixel 32bppPBGRA. Per convertire il formato di immagine, utilizzare il metodoCreateFormatConverterper creare un oggetto IWICFormatConverter. Dopo la creazione, usare il metodo Initialize per eseguire la conversione.
// 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 ); }
Creare un oggetto ID2D1RenderTarget per il rendering in un handle di finestra.
// 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 );
Per altre informazioni sulle destinazioni di rendering, vedere Panoramica delle destinazioni di rendering di Direct2D .
Creare un oggetto ID2D1Bitmap usando il metodo 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); }
Disegna il ID2D1Bitmap usando il metodo D2D ID2D1RenderTarget::DrawBitmap.
// Draws an image and scales it to the current window size if (m_pD2DBitmap) { m_pRT->DrawBitmap(m_pD2DBitmap, rectangle); }
Il codice è stato omesso da questo esempio. Per il codice completo, vedere l'esempio WIC Image Viewer Using Direct2D.
Vedere anche