Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically setting a PNG to a Picture Control in Win32 APIs

Tags:

c

winapi

I use Visual Studio 2008, I have the PNG file loaded in the Resource View, assigned it IDB_BANG_PNG.

The Picture Control is called IDC_STATIC15.

I am having trouble trying to get the PNG loaded into the picture control.

LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

// Way of loading a bmp with a mask perhaps?  Or a PNG file programatically?

static HBRUSH hBrushStatic;
HBITMAP hBmp = LoadBitmap(hDlg,MAKEINTRESOURCE(IDB_BANG_PNG));

switch(message)
{
case WM_INITDIALOG:     
    CheckDlgButton(hDlg, IDC_CHECK, FALSE);
    EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
    // Bitmap version is IDB_BANG, PNG is at IDB_BANG_PNG
    // IDC_STATIC15 is the picture frame
    HWND item = GetDlgItem(hDlg,IDC_STATIC15);
    SendMessage(item,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hBmp);   
    return TRUE;

// .... snip

I am rather naive when it comes to Win32/GUI development, doing a quick project and got stuck her, any help is appreciated.

like image 376
Jon Weinraub Avatar asked Oct 07 '09 16:10

Jon Weinraub


2 Answers

This works using GDI+ and the bitmap class :

Bitmap oBmp(L"D:\\test.png");
HBITMAP hBmp;
oBmp.GetHBITMAP(0, &hBmp);
SendMessage(item,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hBmp);

Some caveats.Your control needs a SS_BITMAP style. Don't forget to include gdiplus.h and its library. You need to initialize (GdiplusStartup) and shutdown GDI+. Freeing all system resources is on you.

like image 153
anno Avatar answered Nov 10 '22 01:11

anno


I don't think LoadBitmap or any other plain GDI function (such as LoadImage) will load a PNG.

You can save your resource to a 32-bit BMP with an image editing tool and then use LoadImage with LR_CREATEDIBSECTION.

Or you can use a library that will load the PNG file into a DIBSECTION. GDI+ will load PNG and JPG in addition to BMP.

The OLE automation libraries also have an IPicture interface that you can instantiate with various image types and then get the underlying DIBSECTION. It's awkward to use, especially if you're not familiar with COM. See OleLoadPicture for a starting point.

like image 22
Adrian McCarthy Avatar answered Nov 10 '22 00:11

Adrian McCarthy