I'm having difficulty setting the bfSize and biSizeImage header values. I should have the correct formulas for both values, however when I write either value to the output file I lose the leftmost byte. The problem may have to do with the fputc() function expecting an unsigned char.

Below is the code and some temporary print statements which output the hex values of biSizeImage and bfSize:

// set bitmap width of output file
int outBiWidth = inBiWidth * factor;
fseek(outptr, 18, SEEK_SET);
fputc(outBiWidth, outptr);

// set bitmap height of output file
int outBiHeight = inBiHeight * factor;
fseek(outptr, 22, SEEK_SET);
fputc(outBiHeight, outptr);

// set bitmap image size of output file
int outBiSizeImage = ((outBiWidth * sizeof(RGBTRIPLE)) + outpadding) * abs(outBiHeight);
fseek(outptr, 34, SEEK_SET);
fputc(outBiSizeImage, outptr);
printf("Out Image Size: %x\n", outBiSizeImage);

// set file size of output file
int outBfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + outBiSizeImage;
fseek(outptr, 2, SEEK_SET);
fputc(outBfSize, outptr);
printf("Out File Size: %x\n", outBfSize);

When I execute resize, the print statements show the correct hex values, however the comparison between the output file and the staff's file tells a different story:


Help is much appreciated, thanks!

  • Problem solved, copy bi and bf to new biout and bfout variables with BITMAPINFOHEADER and BITMAPFILEHEADER data types. From there, set the values biout.biSizeImage and bfout.bfSize.
    – corvid
    Jul 12, 2019 at 1:08

1 Answer 1


Well, that's an unusual way to try and do that. You're writing to the headers in the file! Seems like the hard way to update the headers.

Don't you think it would be easy to read the headers into a struct in memory, modify the appropriate values in memory, and then write the header structs out to the output file?

Here's an efficiency tip. Making changes in memory is universally faster than rewriting data in a file, especially a spinning hard disk (up to 1000 times or more faster), and eliminates a LOT of overhead that's done under the covers.

There's also the issue that the code is using fputc to write out the data. The vars that contain the data are of type int, but fputc() writes an unsigned char. These two data types are different in size, type and signage. I'll put this in the unpredictable results category.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Hey, thanks for the explanation! I noticed that in the file provided, the padding was being written to the file with hex value 0x00. Previously I didn’t understand the efficiency gap between writing to a file and writing to memory so it didn’t seem unreasonable for me to try out fputc().
    – corvid
    Jul 12, 2019 at 2:57
  • 1
    Also, I made two new structs and set the output file values with statements such as biout.biSizeImage. Definitely much easier than what I initially wrote -
    – corvid
    Jul 12, 2019 at 3:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .