Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a image into a string in c++ source code

Tags:

c++

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.

like image 451
Yukhy Avatar asked Sep 08 '25 10:09

Yukhy


1 Answers

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.

like image 75
Rick Smith Avatar answered Sep 11 '25 07:09

Rick Smith