Exemplo n.º 1
0
HRESULT ConvertBitmapSource( RECT rcDest, IWICBitmapSource*& pToRenderBitmapSource )
{
	HRESULT hr = S_OK;
	IWICBitmapScaler* pScaler = nullptr;
	WICPixelFormatGUID pxformat;
	IWICFormatConverter* pConverter = nullptr;

	pToRenderBitmapSource = nullptr;

	// Get the client Rect
	//RECT rcClient = rcDest;
	//hr = GetClientRect(hWnd, &rcClient) ? S_OK: E_FAIL;

	if( SUCCEEDED( hr ) )
	{
		// Create a BitmapScaler
		hr = g_UserImageFactoryInst.m_pIWICFactory->CreateBitmapScaler( &pScaler );
		//hr = IWICImagingFactory_CreateBitmapScaler( g_UserImageFactoryInst.m_pIWICFactory, &pScaler );

		// Initialize the bitmap scaler from the original bitmap map bits
		if( SUCCEEDED( hr ) )
		{
			pScaler->Initialize( g_UserImageFactoryInst.m_pOriginalBitmapSource,
								 rcDest.right - rcDest.left,
								 rcDest.bottom - rcDest.top,
								 WICBitmapInterpolationModeFant );
		}

		//hr = IWICBitmapScaler_GetPixelFormat( pScaler, &pxformat );
		hr = pScaler->GetPixelFormat( &pxformat );

		// Format convert the bitmap into 32bppBGR, a convenient 
		// pixel format for GDI rendering 
		if( SUCCEEDED( hr ) )
		{
			//hr = IWICImagingFactory_CreateFormatConverter( g_UserImageFactoryInst.m_pIWICFactory, &pConverter );
			hr = g_UserImageFactoryInst.m_pIWICFactory->CreateFormatConverter( &pConverter );

			// Format convert to 32bppBGR
			if( SUCCEEDED( hr ) )
			{
				hr = pConverter->Initialize( static_cast<IWICBitmapSource*>( pScaler ),	// Input bitmap to convert
											 GUID_WICPixelFormat32bppBGR,				//	&GUID_WICPixelFormat32bppBGR,
											 WICBitmapDitherTypeNone,					// Specified dither patterm
											 NULL,										// Specify a particular palette 
											 0.f,										// Alpha threshold
											 WICBitmapPaletteTypeCustom );				// Palette translation type

				// Store the converted bitmap as ppToRenderBitmapSource 
				if( SUCCEEDED( hr ) )
					pConverter->QueryInterface( IID_IWICBitmapSource, reinterpret_cast<void**>( &pToRenderBitmapSource ) );
			}
			SAFE_RELEASE( pConverter );
		}

		SAFE_RELEASE( pScaler );
	}

	return hr;
}