CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2009
    Posts
    37

    PNG decoding and splash screen...

    I am a big Java developer with C experience, everything I do OO is in Java and use C with JNI for anything I cannot do in Java. (Just a heads up on where I am coming from). In one of my applications I need to show a splash screen in C prior to running the Java stuff. I have found/copied/pasted/modified etc the code to do transparent splash screens based off a PNG file using the Windows Imaging Component. The problem is this is only available on Vista and up, I know you can install WIC on XP but is there an easier way to decode PNG's in XP without WIC so I can use them in my application. Here are my two main methods that do the work:

    Code:
    // Loads a PNG image from the specified stream (using Windows Imaging Component).
    IWICBitmapSource * LoadBitmapFromStream(IStream * ipImageStream)
    {
        // initialize return value
        IWICBitmapSource * ipBitmap = NULL;
     
        // load WIC's PNG decoder
        IWICBitmapDecoder * ipDecoder = NULL;
        if (FAILED(CoCreateInstance(CLSID_WICPngDecoder, NULL, CLSCTX_INPROC_SERVER, __uuidof(ipDecoder), reinterpret_cast<void**>(&ipDecoder))))
            goto Return;
    
    	// load the PNG
        if (FAILED(ipDecoder->Initialize(ipImageStream, WICDecodeMetadataCacheOnLoad)))
            goto ReleaseDecoder;
     
        // check for the presence of the first frame in the bitmap
        UINT nFrameCount = 0;
        if (FAILED(ipDecoder->GetFrameCount(&nFrameCount)) || nFrameCount != 1)
            goto ReleaseDecoder;
     
        // load the first frame (i.e., the image)
        IWICBitmapFrameDecode * ipFrame = NULL;
        if (FAILED(ipDecoder->GetFrame(0, &ipFrame)))
            goto ReleaseDecoder;
     
        // convert the image to 32bpp BGRA format with pre-multiplied alpha
        //   (it may not be stored in that format natively in the PNG resource,
        //   but we need this format to create the DIB to use on-screen)
        WICConvertBitmapSource(GUID_WICPixelFormat32bppPBGRA, ipFrame, &ipBitmap);
        ipFrame->Release();
     
    ReleaseDecoder:
        ipDecoder->Release();
    Return:
        return ipBitmap;
    }
    
    // Creates a 32-bit DIB from the specified WIC bitmap.
    HBITMAP CreateHBITMAP(IWICBitmapSource * ipBitmap)
    {
        // initialize return value
        HBITMAP hbmp = NULL;
     
        // get image attributes and check for valid image
        UINT width = 0;
        UINT height = 0;
        if (FAILED(ipBitmap->GetSize(&width, &height)) || width == 0 || height == 0)
            goto Return;
     
        // prepare structure giving bitmap information (negative height indicates a top-down DIB)
        BITMAPINFO bminfo;
        ZeroMemory(&bminfo, sizeof(bminfo));
        bminfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        bminfo.bmiHeader.biWidth = width;
        bminfo.bmiHeader.biHeight = -((LONG) height);
        bminfo.bmiHeader.biPlanes = 1;
        bminfo.bmiHeader.biBitCount = 32;
        bminfo.bmiHeader.biCompression = BI_RGB;
     
        // create a DIB section that can hold the image
        void * pvImageBits = NULL;
        HDC hdcScreen = GetDC(NULL);
        hbmp = CreateDIBSection(hdcScreen, &bminfo, DIB_RGB_COLORS, &pvImageBits, NULL, 0);
        ReleaseDC(NULL, hdcScreen);
        if (hbmp == NULL)
            goto Return;
     
        // extract the image into the HBITMAP
        const UINT cbStride = width * 4;
        const UINT cbImage = cbStride * height;
        if (FAILED(ipBitmap->CopyPixels(NULL, cbStride, cbImage, static_cast<BYTE *>(pvImageBits))))
        {
            // couldn't extract image; delete HBITMAP
            DeleteObject(hbmp);
            hbmp = NULL;
        }
     
    Return:
        return hbmp;
    }

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: PNG decoding and splash screen...

    So I'm not sure what the problem is here. Is it decoding the PNG? Why not use a BMP instead?

  3. #3
    Join Date
    Feb 2009
    Posts
    37

    Re: PNG decoding and splash screen...

    Well as it sits it works fine in Windows Vista and 7 as I am using the Windows Imaging Component (WIC), and this comes standard on Vista and up however in XP is where the problem is. I need to show be able to create an HBITMAP from a PNG file in XP without using WIC thus this example works fine just not in XP, I am looking for an alternative. Thanks.

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: PNG decoding and splash screen...

    I thought maybe you could just convert your PNG to BMP and use that from a resource (MS invented BMP after all). There's CImage if you're using MFC/ATL. It handles PNG just fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured