Jon Weinraub
Jon Weinraub

Reputation: 394

Programatically setting a PNG to a Picture Control in Win32 APIs

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.

Upvotes: 6

Views: 9375

Answers (4)

Adrian McCarthy
Adrian McCarthy

Reputation: 47962

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.

Upvotes: 1

Hernán
Hernán

Reputation: 4587

Have you checked the DevIL/OpenIL library? It follows the OpenGL style of function naming and is written in the good ol' C.

I've used in the past, successfully. It's very portable also.

Check it out here: http://openil.sourceforge.net/features.php

Upvotes: 0

anno
anno

Reputation: 5989

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.

Upvotes: 4

Nick Dandoulakis
Nick Dandoulakis

Reputation: 43120

Personally, I use CPictureEx class. I think it doesn't support png, but bmp, jpeg and animated gif.

I also use Cairo graphics for special rendering. Cairo supports png format.
Of course Cairo is a bit harder to use.

Upvotes: 0

Related Questions