Friday, May 6, 2011

Win32 CreateBitmap() example c c++

Following code fragment shows how to output an PNG image using Bitmap created by
CreateBitmap() function. Here, CPNGWrapper class is just a wrapper for libpng which does the chores on behalf of the user. After loading a PNG image, we create a bitmap by passing the image information(width, height, buffer) to CreateBitmap() Function. Then the Bitmap content is transferred to current window by BitBlt function.



                CPNGWrapper        *pPNG = new CPNGWrapper("./Manchuria.png");
                HDC                hDC = GetDC(hWnd);
                HDC                hMemDC = CreateCompatibleDC(hDC);
                HBITMAP            hOldBMP, hNewBMP;

                pPNG->read();
                pPNG->convert(PNG_PIXEL_BGRA_32);

                // Display Image onto Screen for validation.
                //
                hNewBMP = CreateBitmap(pPNG->getWidth(), pPNG->getHeight(), 1, 32, pPNG->getBuffer());
                hOldBMP = (HBITMAP)SelectObject(hMemDC, hNewBMP);

                BitBlt(hDC, 0, 0, pPNG->getWidth(), pPNG->getHeight(), hMemDC, 0, 0, SRCCOPY);

                SelectObject(hMemDC, hOldBMP);
                DeleteObject(hNewBMP);
                DeleteDC(hMemDC);
                ReleaseDC(hWnd, hDC);
                delete pPNG;