리소스에서 비트맵을 로드하는 방법(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 를 사용하여 이미지의 프레임 수를 확인합니다.
참고 항목