tags:

views:

1294

answers:

4

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.

A: 

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.

Nick D
This project I am working on is in C, not C++. Classes, CImage, MFC, et cetera won't be possible and won't compile.How do I use paint the bmp into a picture control, in a dialog at runtime?
Jonathan
+1  A: 

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.

anno
A: 

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

Hernán
+1  A: 

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.

Adrian McCarthy