What do I have to do to get a bitmap from a png file? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Ankou
Experienced Solver
Experienced Solver
Posts: 57
Joined: Fri Feb 29, 2008 9:59 pm
Location: Germany

What do I have to do to get a bitmap from a png file?

Post by Ankou » Sun Mar 02, 2008 11:27 am

Because I always get an error, that he couldn't find the file in the rc file.
I don't event know how he should find an image from a string in an resourcefile, I always thought you would need an ID.
And when I'm using wxBITMAP_TYPE_PNG I get an "No Image Handler for type 15 defined"

tan
Moderator
Moderator
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan » Sun Mar 02, 2008 12:04 pm

Hi,
AFAIK you can't use PNG files in MS resource files.
I can suggest 3 way:
1. Convert PNG file to BMP
2. Load PNG file on runtime using wxBitamp::LoadFile(...)
3. Convert PNG file to binary data using bin2c:
http://forums.wxwidgets.org/viewtopic.php?t=15176
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10

Ankou
Experienced Solver
Experienced Solver
Posts: 57
Joined: Fri Feb 29, 2008 9:59 pm
Location: Germany

Post by Ankou » Sun Mar 02, 2008 12:29 pm

you can, you have to choose RCDATA as Ressoucetype(at least when using DX), but how do I get an wxBitmap from it?
But I tried it with LoadFile and then I get those errors.

tan
Moderator
Moderator
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan » Sun Mar 02, 2008 1:38 pm

Ankou wrote: But I tried it with LoadFile and then I get those errors.
Did you call ::wxInitAllImageHandlers() (or wxImage::AddHandler
(new wxPNGHandler())) before?
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10

idhan
Experienced Solver
Experienced Solver
Posts: 88
Joined: Tue Feb 28, 2006 9:03 pm

Post by idhan » Sun Mar 02, 2008 1:39 pm

Hi,

Probably you didn't call these lines:

wxImage::AddHandler(new wxBMPHandler);
wxImage::AddHandler(new wxPNGHandler);

you should do it at the beginning of you application.

Ankou
Experienced Solver
Experienced Solver
Posts: 57
Joined: Fri Feb 29, 2008 9:59 pm
Location: Germany

Post by Ankou » Sun Mar 02, 2008 1:50 pm


phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Sun Mar 02, 2008 2:43 pm

Then you have to link the correct libraries or show us the linker output.

mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r » Sun Mar 02, 2008 7:30 pm

tan wrote:AFAIK you can't use PNG files in MS resource files.
You can put any file type in a MS resource file.

from one of my projects

Code: Select all

SplashImage PNG "splash.png"
Code to load the png from the ms resource
Note: This is not my code but I forget who to attribute it to. I believe I found it on a thread here or at the wxWidgets wiki. But could be from some random google as well.

Code: Select all

wxMemoryInputStream *GetResourceInputStream(wxString resource_name, wxString resource_type){
	HRSRC hrsrc=FindResource(wxGetInstance(), resource_name, resource_type);
	if(hrsrc==NULL) return NULL;

	HGLOBAL hglobal=LoadResource(wxGetInstance(), hrsrc);
	if(hglobal==NULL) return NULL;

	void *data=LockResource(hglobal);
	if(data==NULL) return NULL;

	DWORD datalen=SizeofResource(wxGetInstance(), hrsrc);
	if(datalen<1) return NULL;

	return new wxMemoryInputStream(data, datalen);
}

bool HasResource(wxString resource_name, wxString resource_type){
	HRSRC hrsrc=FindResource(wxGetInstance(), resource_name, resource_type);
	if(hrsrc==NULL) return false;

	HGLOBAL hglobal=LoadResource(wxGetInstance(), hrsrc);
	if(hglobal==NULL) return false;

	return true;
}
My code for loading the png using the above function

Code: Select all

// create the main application window
    wxMemoryInputStream* stream = GetResourceInputStream(wxT("SplashImage"), wxT("PNG"));
    if(stream != NULL){
		wxImage image(*stream);
		wxBitmap bitmap(image);

		if (bitmap.Ok()){
			 
			wxSplashScreen *splash = new wxSplashScreen(bitmap, wxSPLASH_CENTRE_ON_SCREEN|wxSPLASH_TIMEOUT,
				2000, frame, wxID_ANY, wxDefaultPosition, wxDefaultSize,
				wxBORDER_NONE|wxSTAY_ON_TOP);
			wxYield();

		}
	}
-Max

tan
Moderator
Moderator
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan » Mon Mar 03, 2008 6:27 am

@mc2r:
many thanks for your code :)
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10

Post Reply