Manually writing bmp image works for one resolution and is broken for other <p>I try to create bmp output image. According to bmp header structure this code looks correct. Bmp header structure was taking according to <a href="http://www.fastgraph.com/help/bmp_header_format.html" rel="nofollow">http://www.fastgraph.com/help/bmp_header_format.html</a> But it produced valid bmp image only for resolution 500x500px. Whats wrong with code?</p> <pre><code>#include &lt;cstdint&gt; #include &lt;fstream&gt; struct BmpHeader { uint32_t mFileSize; // Size of file in bytes uint32_t mReserved01; // 2x 2 reserved bytes uint32_t mDataOffset; // Offset in bytes where data can be found (54) uint32_t mHeaderSize; // 40B int mWidth; // Width in pixels int mHeight; // Height in pixels short m_colorPlates; // Must be 1 short mBitsPerPixel; // We use 24bpp uint32_t mCompression; // We use BI_RGB ~ 0, uncompressed uint32_t mImageSize; // mWidth x mHeight x 3B uint32_t mHorizRes; // Pixels per meter (75dpi ~ 2953ppm) uint32_t mVertRes; // Pixels per meter (75dpi ~ 2953ppm) uint32_t mPaletteColors; // Not using palette - 0 uint32_t mImportantColors; // 0 - all are important }; int main() { uint32_t width = 2; uint32_t height = 2; std::ofstream bmp("1.bmp", std::ios::binary); BmpHeader header; bmp.write("BM", 2); header.mFileSize = uint32_t(sizeof(BmpHeader) + 2) + width * height * 3; header.mReserved01 = 0; header.mDataOffset = uint32_t(sizeof(BmpHeader) + 2); header.mHeaderSize = 40; header.mWidth = width; header.mHeight = height; header.m_colorPlates = 1; header.mBitsPerPixel = 24; header.mCompression = 0; header.mImageSize = 0; header.mHorizRes = 2953; header.mVertRes = 2953; header.mPaletteColors = 0; header.mImportantColors = 0; bmp.write((char*)&amp;header, sizeof(header)); for (unsigned int y = 0; y&lt;height; y++) { for (unsigned int x = 0; x&lt;width; x++) { typedef unsigned char byte; byte bgrB[3]; bgrB[0] = 120; bgrB[1] = 120; bgrB[2] = 120; bmp.write((char*)&amp;bgrB, sizeof(bgrB)); } } } </code></pre>