This question has already been answered:

Hello! Tell me, can this be done only through resources or something else? If how else can you give an example?

Reported as a duplicate by members of sercxjo , Kromster , αλεχολυτ , aleksandr barakin , user207618 10 Oct '16 at 23:26 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • 2
    Through resources, it is universal and simpler, because this resource can always be replaced and it will not require rebuilding the entire project. As for a simple Bitmap, it can also be loaded from the buffer in memory , and the initial image (any graph. Format) can always be represented as a raster (set of bytes in the array). - mega
  • I draw the bitmap and buffer size, and in the resources there are some sort of winery used bitmap rendering functions, they don’t suit me. I read the links like they say what they need, and the code is not that - helldrg

2 answers 2

I will try to explain in detail the introduction of an external file in the data. So, part 1. We write a program that reads a file in binary (not text mode !!), and writes each byte as an element of an array. See, for example, this code .

Part 2. Apply this program to an external file, we get a file with a description of the array. Type unsigned char img[] = { 0x02, 0x15, ... Even better, add immediately a second variable of type int img_len = ...

Part 3. Paste this file into the project. Where necessary - refer to this array; for example, to write it back to the file -

 FILE * f = fopen("out","wb"); if (f) { fwrite(f,img,1,img_len); fclose(f); } 

Like that.

  • I have an error in 3 parts, the contents of img are somewhat different from the contents of the function passed to ReadFile - helldrg
  • Understood, I forgot to allocate memory through VirtualAlloc Thank you very much! - helldrg

There is an old-fashioned way. Having a file of the image, generate on it the code of the declaration of the array of the form

 const char a[] = { ... }; 

where ... is data from a file, of course, in the form of correctly written constants. Get an array identical to the contents of the image file. The type of the array may be different, depending on which one is more convenient to use.

Then just include this file in the project, use the array as external. Alternatively, it is possible to place in the array not the entire contents of the file, for example, without a header, etc. depending on the image format.

  • fwrite ((void *) fileContent.Contents, fileContent.ContentsSize, 1, fp); fwrite (& fileContent.ContentsSize, sizeof (fileContent.ContentsSize), 1, fp); So I wrote down the content and size of the content in the file and I will need to write it to const char a [] = {...} ;? And in the file in a binary form to write or in the usual? - helldrg
  • @helldrg The bad news is that if this image is not needed, it will still stick in the memory. And the code - well, something like vpaste.net/LQpvw Replace only with your file and array names. - Harry
  • About the fact that they will occupy the memory is nothing terrible! Thank you very much, and lastly: ReadFile (FileHandle, fileContent.Contents, FileSize32, & BytesRead, 0); I usually read the data from the image with this function, if applied to this case: fileContent.Contents is the same as the character array FileSize32 is the number of bytes that the character array occupies? - helldrg
  • @Harry fileContent.ContentsSize = sizeof (img_background) -1; memcpy ((void *) fileContent.Contents, & img_background, fileContent.ContentsSize); I try to write - it does not work - helldrg
  • @helldrg Put somewhere full source, so incomprehensible. And more - and what did not suit my version, why only through the API, and not the standard C functions? - Harry