4

it's posible save a image into a c++ source code?

#include <string>

int main(){
std::string = "<full code of a .png for example>";

//...
}

The problem it's that a image got a lot of characters like '\'... and copy and pasting it from a hexadecimal editor generates errors.

I don't want load the image from a .png file, i want get the image code directly into a string.

Yukhy
  • 69
  • 1
  • 6

3 Answers3

4

This is generally done by saving the image into a base64 encoded string. It requires more bytes to store, but has the advantage of being a string. You can use an online tool to convert your image to a base64 encoded string that you can copy into your source file.

string base64 = "copy encoded string here";

See this question for more details on how to decode that string into an image. Hope this helps.

Community
  • 1
  • 1
Rick Smith
  • 9,031
  • 15
  • 81
  • 85
1

There are tools like xxd which will generate a character array in a header file from a binary input file for you to include in your project, see this answer. This is generally preferable for this use case to using a string since you don't need to worry about base64 encoding to handle special characters.

Community
  • 1
  • 1
mattnewport
  • 13,728
  • 2
  • 35
  • 39
  • I will try this too, it's possible that will be better that i was looking for. Thank you too! – Yukhy Aug 25 '15 at 18:17
0

Add the image to your resources if using Windows. Console applications can also have resource files. Then just load the image from the resources.

The other option is to use Base64 encoding on the image, copy the string into your source, recompile and decode the string at runtime..

Brandon
  • 22,723
  • 11
  • 93
  • 186