Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!

I have the following question:

I have some .png files, which i want to Display in a CWnd in a given Rect.

I found some code, which reads the BGRA values of one .png-File into an array and then creates a CBitmap out of this array and then displays the CBitmap.

Is there any disadvantage of doing this? Is there a better way of doing it?

Is there a ShowPng function somewhere?:-)

thanks for your help!
Posted

Try examining the code on the following CodeProject articles (the first one in particular is very popular). Compare and contrast.

CxImage[^]

A user draw button that supports PNG files with transparency, for Visual C++ 6.0 and VS2005[^]

Loading JPG & PNG resources using GDI+[^]
 
Share this answer
 
Comments
Member 10781325 18-Feb-15 11:35am    
thx for you anwser!
No, there is no such function in pure C++ and MFC. What you are doing is absolutely correct. First convert the PNG into a memory representation and then form a bitmap out of it. Even if there was a library function in MFC, it had to do it in the same way.

If you are using that image often, you should think about caching the memory representation so that you don't have to read the file from disk multiple times.
 
Share this answer
 
Comments
Member 10781325 18-Feb-15 11:34am    
thank's very much! That's exactly what i wanted to know!:-)

btw: by caching the memory you mean allocating memory with e.g. new or malloc?
or is there even a better place to hold it?

thx alot!
nv3 18-Feb-15 11:51am    
Exactly. I would allocate space by new for those images that are used frequently in you program and delete those at the end of the program run. You might want to write a little helper class for that purpose that not only takes care of the automated delete, put also of the conversion from png-format to bitmap. It might even take usage counts of your images and for instance keep an image in memory if it is used three times or more.
CHill60 18-Feb-15 12:34pm    
Good point on the caching. 5
nv3 18-Feb-15 12:53pm    
Thanks!
Member 10781325 19-Feb-15 2:25am    
Ok, thanks alot for your help! I will solve the Problem with a helper class:-)
With GDI+ is simple matter, see "Loading and Displaying Bitmaps"[^] at MSDN.
That's together with GdiplusStartup/GdiplusShutdown (sample usage here: "Drawing a Line"[^] at MSDN) is all you need.
 
Share this answer
 
Comments
Member 10781325 18-Feb-15 11:35am    
thx for hint!
Sergey Alexandrovich Kryukov 18-Feb-15 11:58am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900