//create new BITMAPINFOHEADER details
    bi_new.biHeight = bi.biHeight * factor;
    bi_new.biWidth = bi.biWidth * factor;
    // determine padding for infile scanlines
    int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    // determine padding for outfile scanlines
    int new_padding = (4 - (bi_new.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    bi_new.biSizeImage = (bi_new.biHeight * bi_new.biWidth * 9) + (new_padding * bi_new.biHeight);

    //create new BITMAPFILEHEADER details
    bf_new.bfSize = bi_new.biSizeImage + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);

    // write outfile's BITMAPFILEHEADER
    fwrite(&bf_new, sizeof(BITMAPFILEHEADER), 1, outptr);

    // write outfile's BITMAPINFOHEADER
    fwrite(&bi_new, sizeof(BITMAPINFOHEADER), 1, outptr);

I have written code for new header files of bit map. Please check this if it is correct, I guess something is going wrong. I didnt write code to resize the pixels yet. But, I have run my code, found that new bmp file is generated and I couldnt open the file. It says unsupported file format. I am not sure if this is expected behaviour, Please let me know if something's wrong in my code. Thank you!


You'd have to copy the other fields from bi to bi_new. Easiest way is to initialize bi_new using


which copies the whole structure, you'd still have to change the fields related to the size.

Same applies to bf_new, which should be initialized to bf.

Your calculation for biSizeImage looks a bit off. It should be the number of bytes in a line (including padding) times the number of lines. Your first part is off by a factor of 3 (better use sizeof(RGBTriple) for clarity where that factor came from), and it would work only for positive heights. The number of lines may be negative, in which case the image is stored upside down, and the number of lines is the height's absolute value.

  • Thank you! I fixed it. Initialization was a blunder. I have used bi_new.biSizeImage = (bi_new.biHeight * bi_new.biWidth * 3) + (new_padding * bi_new.biHeight); for new iamge size and it works. The formula as is follows, number of pixels in new image times size of RGBTriple i.e 3 bytes + new padding per line. – Harshat Reddy Feb 22 '17 at 12:14
  • Does this also work for negative values of bi_new.biHeight, which just mean the image is stored upside down? (I think there should be abs(bi_new.biHeight) in this formula) – Blauelf Feb 22 '17 at 12:25
  • Oh, I don't know about that. I think it doesn't works. I will all the abs() method. Thanks again! – Harshat Reddy Feb 22 '17 at 12:28
  • A question, I understood that biWidth indicates width of image in pixels. My doubt is that, does this width indicates the number of pixels including padding or number of pixles excluding padding? – Harshat Reddy Feb 22 '17 at 12:42
  • Padding is given in bytes, not in pixels, so that makes no sense. Each pixel is sizeof(RGBTRIPLE), or 3 bytes. – Blauelf Feb 22 '17 at 14:57

You must log in to answer this question.

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