Direct2D を使用して BitmapSource を描画する方法

このトピックでは、Direct2D を使用して IWICBitmapSource を 描画するプロセスについて説明します。

Direct2D を使用してビットマップ ソースを描画するには

  1. ソース イメージをデコードし、ビットマップ ソースを取得します。 この例では、 IWICBitmapDecoder を使用してイメージをデコードし、最初のイメージ フレームを取得します。

       // 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);
       }
    

    描画するビットマップ ソースのその他の種類については、「 ビットマップ ソースの概要」を参照してください。

  2. ビットマップ ソースを 32bppPBGRA ピクセル形式に変換します。

    Direct2D でイメージを使用するには、そのイメージを 32bppPBGRA ピクセル形式に変換する必要があります。 イメージ形式を変換するには、 CreateFormatConverter メソッドを使用して IWICFormatConverter オブジェクトを作成します。 作成したら、 Initialize メソッドを使用して変換を実行します。

       // 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
               );
       }
    
  3. ウィンドウ ハンドルにレンダリングするための ID2D1RenderTarget オブジェクトを作成します。

       // 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
           );
    

    レンダー ターゲットの詳細については、「Direct2D レンダー ターゲットの概要」を参照してください。

  4. ID2D1RenderTarget::CreateBitmapFromWicBitmap メソッドを使用して ID2D1Bitmap オブジェクトを作成します。

        // 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);
        }
    
  5. D2D ID2D1RenderTarget::D rawBitmap メソッドを使用して ID2D1Bitmap を描画します。

        // Draws an image and scales it to the current window size
        if (m_pD2DBitmap)
        {
            m_pRT->DrawBitmap(m_pD2DBitmap, rectangle);
        }
    

この例では、コードは省略されています。 完全なコードについては、「 Direct2D サンプルを使用した WIC イメージ ビューアー」を参照してください。

参照

プログラミング ガイド

参照

サンプル

Direct2D