4

I'm looking for a way to embed an image in a library (Windows-only). I don't want to go the 'traditional' way of putting it in the resources (because of special circumstances that make it not so convenient to mess around with the resource handle.

Ideally, there would be something like xpm files: a 'text' representation of an image that is put in a c array and that some code converts into a bitmap in memory, which can then somehow be loaded into an HIMAGE or an HICON. The images I want to embed are 32-bit bitmaps (bmp). Any ideas? I'm using MFC so an MFC library would be fine, but of course I can use a library that doesn't use MFC too. Thanks.

Roel
  • 19,338
  • 6
  • 61
  • 90

3 Answers3

9

Google for a bin2c utility (something like http://stud3.tuwien.ac.at/~e0025274/bin2c/bin2c.c). It takes a file's binary representation and spits out a C source file that includes an array of bytes initialized to that data.

Just link the file in and you have your image sitting in a chunk of memory.

Using this kind of tool is really common on embedded systems where such things as 'resources' and even files might not exist.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 1
    Worked great, thanks. For completeness: to load data from memory, I used some code from http://www.ragestorm.net/blogs/?p=9 which revolves around LookupIconIdFromDirectoryEx() and CreateIconFromResourceEx() (for HICONs). – Roel Oct 30 '08 at 14:20
  • 2
    I would suggest using `xxd -i myimage.png > myimage.h` instead. Note that `xdd` is a Linux util. – gberth Oct 01 '20 at 13:49
6

The Gimp can export to C files. I think that would be the easiest way to do it.

Leon Timmermans
  • 30,029
  • 2
  • 61
  • 110
2

The open source application Hexy is designed specifically for this. It runs on windows and linux. https://github.com/tristan2468/Hexy

  • Is it different from bin2c? Is there a binary somewhere? – Roel May 17 '11 at 13:53
  • The binaries are in that GitHub link, there's a "Downloads" button on the right. Hexy is GUI based, so you just drag and drop all the files you want to convert onto it. – tristan_2468 May 18 '11 at 11:20