How do I use IImgCtx to get load an image with an alpha channel?

Posted by fret on Stack Overflow See other posts from Stack Overflow or by fret
Published on 2010-04-16T04:55:42Z Indexed on 2010/04/16 5:03 UTC
Read the original article Hit count: 348

Filed under:

I have some working code that uses IImgCtx to load images, but I can't work out how to get at the alpha channel. For images like .gif's and .png's there are transparent pixels, but using anything other than a 24-bit bitmap as a drawing surface doesn't work.

For reference on the interface: http://www.codeproject.com/KB/graphics/JianImgCtxDecoder.aspx

My code looks like this:

IImgCtx *Ctx = 0;
HRESULT hr = CoCreateInstance(CLSID_IImgCtx, NULL, CLSCTX_INPROC_SERVER, IID_IImgCtx, (LPVOID*)&Ctx);
if (SUCCEEDED(hr))
{
    GVariant Fn = Name;
    hr = Ctx->Load(Fn.WStr(), 0);
    if (SUCCEEDED(hr))
    {
        SIZE  Size = { -1, -1 };
        ULONG State = 0;

        while (true)
        {
            hr = Ctx->GetStateInfo(&State, &Size, false);
            if (SUCCEEDED(hr))
            {
                if ((State & IMGLOAD_COMPLETE) || (State & IMGLOAD_STOPPED) || (State & IMGLOAD_ERROR))
                {
                    break;
                }
                else
                {
                    LgiSleep(1);
                }
            }
            else break;
        }

        if (Size.cx > 0 &&
            Size.cy > 0 &&
            pDC.Reset(new GMemDC))
        {
            if (pDC->Create(Size.cx, Size.cy, 32))
            {
                HDC hDC = pDC->StartDC();
                if (hDC)
                {
                    RECT rc = { 0, 0, pDC->X(), pDC->Y() };
                    Ctx->Draw(hDC, &rc);
                    pDC->EndDC();
                }
            }
            else pDC.Reset();
        }
    }

    Ctx->Release();

Where "StartDC" basically wraps CreateCompatibleDC(NULL) and "EndDC" wraps DeleteDC, with appropriate SelectObjects for the HBITMAPS etc. And pDC->Create(x, y, bit_depth) calls CreateDIBSection(...DIB_RGB_COLORS...). So it works if I create a 24 bits/pixel bitmap but has no alpha to speak of, and it leaves the 32 bits/pixel bitmap blank. Now this interface apparently is used by Internet Explorer to load images, and obviously THAT supports transparency, so I believe that it's possible to get some level of alpha out of the interface. The question is how?

(I also have fall back code that will call libpng/libjpeg/my .gif loader etc)

© Stack Overflow or respective owner

Related posts about transparency