Embedding .png in Visual Studio Project

Posted on 2011-04-25
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?

Question by:johnny_device
    LVL 16

    Expert Comment

    by:Bryan Butler
    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:
    LVL 7

    Accepted Solution

    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.

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

    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.
    LVL 86

    Assisted Solution

    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            

    Open in new window


    Author Closing Comment

    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.



    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now