BeeLabeille
BeeLabeille

Reputation: 184

How can I add text to my bitmap file in c++

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. The stackoverflow post here is the closest thing i've found so far. Unfortunately I still can't figure out the full implementation.

Please note that I would like to avoid the use of any third party libraries. Any help will be appreciated, Thanks.

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);
}

Upvotes: 0

Views: 1115

Answers (0)

Related Questions