How can I add text to my bitmap file

With the code sample below, I have successfully created a .bmp file which when opened simply displays a grey square. Now I would like to add text to the bitmap before saving it. For example: "Hello World" in green should be displayed on top of the grey square.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void SaveImage(const char* filename)
{
    int Width = 470;
    int Height = 470;
    FILE *f;
    unsigned char *img = NULL;
    int triple_area = 3*Width*Height;
    if(img) free(img);
    img = (unsigned char *)malloc(triple_area);
    memset(img,0,sizeof(img));
    int res;
    Assign_Bitmap_Req(triple_area); //Sets up BmpFileHeader, BmpInfoHeader, BmpPadding as unsigned char arrays

    for(int i=0; i<Width; i++)
    {
        for(int j=0; j<Height; j++)
        {
            res = Height - 1 - j;
            img[(i+res*Width)*3+2] = (unsigned char)(100);
            img[(i+res*Width)*3+1] = (unsigned char)(100);
            img[(i+res*Width)*3+0] = (unsigned char)(100);
        }
    }

    f = fopen(filename,"wb");
    fwrite(BmpFileHeader,1,14,f);
    fwrite(BmpInfoHeader,1,40,f);
    int remainder = (4-(Width*3)%4)%4;
    for(int i=0; i<Height; i++)
    {
        fwrite(img+(Width*(Height-i-1)*3),3,Width,f);
        fwrite(BmpPadding,1,remainder,f);
    }
    fclose(f);
}


I guess i would have to start by creating a DIB section as below but beyond that i have no idea how to proceed.

1
2
void *ppvBits = NULL;
HBITMAP bmpHandle = CreateDIBSection(GetDC(0), (BITMAPINFO *)&BmpInfoHeader, DIB_RGB_COLORS, &ppvBits, NULL, 0);


Any help will be appreciated, Thanks.
If you want to draw into a bitmap you need a memory dc using CreateCompatibleDC(...):

https://msdn.microsoft.com/en-us/library/windows/desktop/dd183488%28v=vs.85%29.aspx

where you can draw.

See the example:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd162950%28v=vs.85%29.aspx
Topic archived. No new replies allowed.