[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Embedding .png in Visual Studio Project

Posted on 2011-04-25
4
Medium Priority
?
727 Views
Last Modified: 2012-05-11
Is there a way of embedding .png images in a Visual Studio 2010 project?  The project currently reads .png images from disk, for the purposes of changing button backgrounds.  This works ok, but means that dozens of separate image files need to be maintained, rather than having the images incorporated into the .exe.  .bmp files can be embedded easily, but do not retain the smooth shading of the original .png, nor the transparency.  I can put them onto a uniform background, and therefore get round the transparency issue, but the images still come out "striated" where there is graduated shading, and thus unusable.

Any tips?

jd
0
Comment
Question by:johnny_device
4 Comments
 
LVL 16

Expert Comment

by:Bryan Butler
ID: 35459905
If it was a silverlight project, I think you could do it.  This link talks about it, but I'm guessing has the same issues that you are seeing:

http://stackoverflow.com/questions/278838/visual-studio-how-to-store-an-image-resource-as-an-embedded-resource
0
 
LVL 7

Accepted Solution

by:
JimBeveridge earned 1600 total points
ID: 35460797
The method varies depending on what kind of project (C++ or .Net).

For a C++, it's possible to do from Visual Studio, but it's about twelve steps long and not worth the effort. Instead, open your RC file in notepad and go to the DESIGNINFO section.  Add a new FILEDATA section so it looks like this. This example shows two PNG files in the rc file, both of which have been copied to the project's res directory. The name of the resource is the same as the name of the file.

/////////////////////////////////////////////////////////////////////////////
//
// FILEDATA
//

FLAG.PNG        FILEDATA                "res\\flag.png"
HAND.PNG       FILEDATA                "res\\hand.png"

/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

Open in new window


Now you need to read it from your C++ code, which also requires some non-obvious code.

HRSRC hrsrc = FindResource(NULL, szFilename, _T("FILEDATA"));
if (!hrsrc) return;
HGLOBAL hglob = LoadResource(NULL, hrsrc);
if (!hglob) return;
LPVOID lpv = LockResource(hglob);
if (!lpv) return;
DWORD dwSize = SizeofResource(NULL, hrsrc);

Open in new window


Having said all of this, 24-bit and 32-bit bitmaps should work fine. If you are having problems with the colors being reduced, then your image lists are probably not configured properly.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 400 total points
ID: 35460828
You can always embed tham as a raw binary resource (RT_RCDATA). You can then use/load them like
HMODULE hLibrary;
HRSRC hResource;
HGLOBAL hResourceLoaded;
LPBYTE lpBuffer;

hLibrary = LoadLibrary("myapp.exe");
if (NULL != hLibrary)
{
    hResource = FindResource(hLibrary, MAKEINTRESOURCE(IID_SOMEPNG), RT_RCDATA);
    if (NULL != hResource)
    {
        hResourceLoaded = LoadResource(hLibrary, hResource);
        if (NULL != hResourceLoaded)        
        {
            lpBuffer = (LPBYTE) LockResource(hResourceLoaded);            
            if (NULL != lpBuffer)            
            {                
                // do something with lpBuffer here            

            }
        }    
    }

    FreeLibrary(hLibrary);
}

Open in new window

0
 

Author Closing Comment

by:johnny_device
ID: 35461497
Thanks for the replies, guys.  I'll split the spoils, because I can learn from more than one answer, but Jim(Beveridge)'s is the fullest response for me.


Regards,

jd
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question