リソースからビットマップを読み込む方法 (Windows イメージング コンポーネント)
このトピックでは、アプリケーション リソースから IWICBitmapFrameDecode を 読み込む方法について説明します。
アプリケーション リソース定義 (.rc) ファイルで、リソースを定義します。 次の例では、 という名前
IDR_SAMPLE_IMAGE
のリソースをImage
定義します。IDR_SAMPLE_IMAGE IMAGE "turtle.jpg"
リソースは、アプリケーションのビルド時にアプリケーションのリソースに追加されます。
アプリケーションからリソースを読み込みます。
HRESULT hr = S_OK; // WIC interface pointers. IWICStream *pIWICStream = NULL; IWICBitmapDecoder *pIDecoder = NULL; IWICBitmapFrameDecode *pIDecoderFrame = NULL; // Resource management. HRSRC imageResHandle = NULL; HGLOBAL imageResDataHandle = NULL; void *pImageFile = NULL; DWORD imageFileSize = 0; // Locate the resource in the application's executable. imageResHandle = FindResource( NULL, // This component. L"SampleImage", // Resource name. L"Image"); // Resource type. hr = (imageResHandle ? S_OK : E_FAIL); // Load the resource to the HGLOBAL. if (SUCCEEDED(hr)){ imageResDataHandle = LoadResource(NULL, imageResHandle); hr = (imageResDataHandle ? S_OK : E_FAIL); }
リソースをロックし、サイズを取得します。
// Lock the resource to retrieve memory pointer. if (SUCCEEDED(hr)){ pImageFile = LockResource(imageResDataHandle); hr = (pImageFile ? S_OK : E_FAIL); } // Calculate the size. if (SUCCEEDED(hr)){ imageFileSize = SizeofResource(NULL, imageResHandle); hr = (imageFileSize ? S_OK : E_FAIL); }
CreateStream メソッドを使用して IWICStream オブジェクトを作成し、イメージ メモリ ポインターを使用して初期化します。
// Create a WIC stream to map onto the memory. if (SUCCEEDED(hr)){ hr = m_pIWICFactory->CreateStream(&pIWICStream); } // Initialize the stream with the memory pointer and size. if (SUCCEEDED(hr)){ hr = pIWICStream->InitializeFromMemory( reinterpret_cast<BYTE*>(pImageFile), imageFileSize); }
CreateDecoderFromStream メソッドを使用して、新しいストリーム オブジェクトから IWICBitmapDecoder を作成します。
// Create a decoder for the stream. if (SUCCEEDED(hr)){ hr = m_pIWICFactory->CreateDecoderFromStream( pIWICStream, // The stream to use to create the decoder NULL, // Do not prefer a particular vendor WICDecodeMetadataCacheOnLoad, // Cache metadata when needed &pIDecoder); // Pointer to the decoder }
デコードされたイメージから IWICBitmapFrameDecode を取得します。
// Retrieve the initial frame. if (SUCCEEDED(hr)){ hr = pIDecoder->GetFrame(0, &pIDecoderFrame); }
このコードは、イメージの最初の (
0
) フレームのみを取得します。 複数フレームのイメージの場合は、 GetFrameCount を 使用して、イメージ内のフレーム数を決定します。
参照