title: C++ How to create a bitmap file
body: <p>I am trying to figure out how to create a bitmap file in C++ VS.  Currently I have taken in the file name and adding the ".bmp" extension to create the file.  I want to know how I could change the pixels of the file by making it into different colors or patterns (ie. like a checkerboard)  This is my function that I have and I believe that I have to send 3 different Bytes at a time in order to establish the color of the pixel.</p>
  
  <pre><code>void makeCheckerboardBMP(string fileName, int squaresize, int n) {
      ofstream ofs;
      ofs.open(fileName + ".bmp");
      writeHeader(ofs, n, n);
  
      for(int row = 0; row &lt; n; row++) {
          for(int col = 0; col &lt; n; col++) {
  
              if(col % 2 == 0) {
                  ofs &lt;&lt; 0;
                  ofs &lt;&lt; 0;
                  ofs &lt;&lt; 0;
              } else {
                  ofs &lt;&lt; 255;
                  ofs &lt;&lt; 255;
                  ofs &lt;&lt; 255;
              }
          }
      }
  }
  
  void writeHeader(ostream&amp; out, int width, int height){
      if (width % 4 != 0) {
          cerr &lt;&lt; "ERROR: There is a windows-imposed requirement on BMP that the width be a          
  multiple of 4.\n";
          cerr &lt;&lt; "Your width does not meet this requirement, hence this will fail.  You can fix     
  this\n";
          cerr &lt;&lt; "by increasing the width to a multiple of 4." &lt;&lt; endl;
          exit(1);
      }
  
      BITMAPFILEHEADER tWBFH;
      tWBFH.bfType = 0x4d42;
      tWBFH.bfSize = 14 + 40 + (width*height*3);
      tWBFH.bfReserved1 = 0;
      tWBFH.bfReserved2 = 0;
      tWBFH.bfOffBits = 14 + 40;
  
      BITMAPINFOHEADER tW2BH;
      memset(&amp;tW2BH,0,40);
      tW2BH.biSize = 40;
      tW2BH.biWidth = width;
      tW2BH.biHeight = height;
      tW2BH.biPlanes = 1;
      tW2BH.biBitCount = 24;
      tW2BH.biCompression = 0;
  
      out.write((char*)(&amp;tWBFH),14);
      out.write((char*)(&amp;tW2BH),40);
  }
  </code></pre>