Loading a bitmap in-code with XPM file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Loading a bitmap in-code with XPM file

  1. #1
    Join Date
    Jul 2011
    Posts
    5

    Loading a bitmap in-code with XPM file

    So here's the deal: I'm trying to load a few bitmaps from within my program's executable, i.e. without loading them from outside files. I looked around for a while for a simple way to do this, and the simplest way seems to be with XPM files. I converted the bitmaps to XPM and included them in my code, but then I realized... I have no idea what to do with it.

    It seems to just define a static char array, with information on the colors and whatnot. So how can I convert it into a bitmap within my program?

    The code for the XPM files looks a bit like this:


    static char *SQUARE_xpm[]={
    "180 180 2 1",
    " c #FF00FF",
    "0 c #FFFFFF",

    <pixel information>

    };

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: Loading a bitmap in-code with XPM file

    What's wrong for you to include those bitmaps as resources and then just call LoadBitmap API?
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2011
    Posts
    5

    Re: Loading a bitmap in-code with XPM file

    Quote Originally Posted by VictorN View Post
    What's wrong for you to include those bitmaps as resources and then just call LoadBitmap API?
    Hundreds of unclear and contradictory explanations on how to do so. If you could help explain it I'd be grateful. Honestly, either way works. I've never used resource files before.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: Loading a bitmap in-code with XPM file

    Quote Originally Posted by Gatleos View Post
    Hundreds of unclear and contradictory explanations on how to do so. If you could help explain it I'd be grateful. Honestly, either way works. I've never used resource files before.
    Well, I never used XPM files and have no idea why I could have to use them....

    As for the resources... You've posted to the forum:
    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.
    so I presume you are using some of the Microsoft IDE (such as VC++6.0, VS2003, VS2005, VS2008, VS2010) to create your applications. Doesn't the IDE you are currently using have a resourced editor?
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2011
    Posts
    5

    Re: Loading a bitmap in-code with XPM file

    Yes, I'm using VC++ 2010 Express. I just haven't been able to find anything to explain exactly what I'm looking for. I just want a bitmap embedded in my program's executable at compile time, that I can access from within the program. Whenever I try to find an explanation on how to do this, I always come up with a tutorial on importing cursors or icons for windows form applications (which my program isn't).

    Of course, I'm probably missing something. I just can't seem to find a straight answer anywhere.

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,523

    Re: Loading a bitmap in-code with XPM file

    Quote Originally Posted by Gatleos View Post
    Yes, I'm using VC++ 2010 Express...
    Unfortunately, Express editions of Visual Studio donít have Resource Editors.
    However, you can manually edit your .rc file. Just add:
    Code:
    IDB_YOUR_ID       BITMAP        "res\\your_file_name.bmp"
    line to it, and define that ID somewhere in your resource.h file (create it if your project doesnít have it).
    Then you can use LoadBitmap() function as Victor suggested above.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  7. #7
    Join Date
    Jul 2011
    Posts
    5

    Re: Loading a bitmap in-code with XPM file

    Yeah, I thought I read somewhere that VC++ Express doesn't have a resource editor.

    So... how exactly do I define a resource ID? I've created a .rc file containing this:

    IDB_ASCII_SQUARE BITMAP "Art\CURSES_SQUARE.bmp"
    IDB_ASCII_TERMINAL BITMAP "ART\CURSES_TERMINAL.bmp"

    and included it under "resources" in my project, then created a resource.h file. I assume this will make my project include the resource files in the executable at compile time. But how do I define these resources in resource.h? Does the .rc file have to be #included by resource.h? What is the scope of the LoadBitmap() function?

    Thanks for the help so far.

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,523

    Re: Loading a bitmap in-code with XPM file

    Quote Originally Posted by Gatleos View Post
    So... how exactly do I define a resource ID? I've created a .rc file containing this:

    IDB_ASCII_SQUARE BITMAP "Art\CURSES_SQUARE.bmp"
    IDB_ASCII_TERMINAL BITMAP "ART\CURSES_TERMINAL.bmp"

    and included it under "resources" in my project, then created a resource.h file. I assume this will make my project include the resource files in the executable at compile time. But how do I define these resources in resource.h? Does the .rc file have to be #included by resource.h? What is the scope of the LoadBitmap() function?
    Yes, you should place
    Code:
    #include "resource.h"
    at the top of your rc file, and wherever you call LoadBitmap() from.
    In the resource.h you should have something like that:
    Code:
    #define IDB_ASCII_SQUARE			101
    #define IDB_ASCII_TERMINAL     			102
    Don't understand your "scope" question.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  9. #9
    Join Date
    Jul 2011
    Posts
    5

    Re: Loading a bitmap in-code with XPM file

    By "scope" I meant where does the LoadBitmap() function have to be called from, but I figured it out. It seems to be from windows.h...

    #including reseource.h in the .rc file causes problems, so I assume you meant the opposite. But now that I've defined the IDs as 101 and 102... how do I use them with the LoadBitmap function? What does the function return?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center