Direct2D: problem on loading (alpha-channel) images from files

  • Thread starter Thread starter GHEORGHE_926
  • Start date Start date
G

GHEORGHE_926

Guest
Hello,

I would have a question on you: I have created a function that loads images from files. Some images contain alpha-channel (.png & .bmp).
The problem: normally, I have to use a converter to be sure that the image can be used in Direct2D, but, if I use a converter, I lose the alpha-channel in the sense that the transparency doesn't work (the portions of the image that is transparent, is black).
If I don't use the converter, everything is ok, the transparency works, but, the 10-15% from all my images appears distorted. So, I have to use a converter.

Question: do you have any idea on how can I improve my function, in order to support transparency/alpha-channel?

Mention: HRESULT variable contains no error.

Thanks a lot!

HRESULT CMyClass::LoadWICBitmapFromFile(PCWSTR path)
{
HRESULT hr = S_OK;

// Create WIC factory
hr = CoCreateInstance(
CLSID_WICImagingFactory1,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&m_pIWICFactory)
);

IWICBitmapDecoder *pIDecoder = NULL;
IWICBitmapFrameDecode *pIDecoderFrame = NULL;
IWICFormatConverter *pConverter = NULL;

hr = m_pIWICFactory->CreateDecoderFromFilename(
path, // Image to be decoded
NULL, // Do not prefer a particular vendor
GENERIC_READ, // Desired read access to the file
WICDecodeMetadataCacheOnDemand, // Cache metadata when needed
&pIDecoder // Pointer to the decoder
);

// Retrieve the first bitmap frame.
if (SUCCEEDED(hr))
{
hr = pIDecoder->GetFrame(0, &pIDecoderFrame);
}

if (SUCCEEDED(hr))
{
hr = m_pIWICFactory->CreateFormatConverter(&pConverter);
}

if (SUCCEEDED(hr))
{
hr = pConverter->Initialize(
pIDecoderFrame,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.0f,
WICBitmapPaletteTypeCustom
);
}

if (SUCCEEDED(hr))
{
hr = m_pIWICFactory->CreateBitmapFromSource(
pConverter/*pIDecoderFrame*/, // If I use pIDecoderFrame instead of pConverter, the alpha channel is preserved, but I have problems on rendering some images since I don't use a converter anymore.
WICBitmapCacheOnDemand,
&pWicBitmap);
}


SafeRelease(pIDecoder);
SafeRelease(pIDecoderFrame);
SafeRelease(pConverter);

return hr;
}

Continue reading...
 
Back
Top