title: C++ bitmap to base64
body: <p>I have the following code to create a bitmap:</p>
  
  <pre><code>//raw data
  PBYTE firstPixel = (PBYTE)((PBYTE)AnsiBdbRecord) + sizeof(WINBIO_BDB_ANSI_381_RECORD);
  
  // declare other bmp structures
  BITMAPFILEHEADER bmfh;
  BITMAPINFOHEADER info;
  RGBQUAD rq[256];
  // create the grayscale palette 
  for (int i = 0; i&lt;256; i++)
  {
      rq[i].rgbBlue = i;
      rq[i].rgbGreen = i;
      rq[i].rgbRed = i;
      rq[i].rgbReserved = 0;
  }
  //RGBQUAD bl = { 0,0,0,0 };  //black color
  //RGBQUAD wh = { 0xff,0xff,0xff,0xff }; // white color
  
  // andinitialize them to zero
  memset(&amp;bmfh, 0, sizeof(BITMAPFILEHEADER));
  memset(&amp;info, 0, sizeof(BITMAPINFOHEADER));
  
  // fill the fileheader with data
  bmfh.bfType = 0x4d42; // 0x4d42 = 'BM'
  bmfh.bfReserved1 = 0;
  bmfh.bfReserved2 = 0;
  bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); // + padding;
  bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD);
  
  // fill the infoheader
  info.biSize = sizeof(BITMAPINFOHEADER);
  info.biWidth = Width;
  info.biHeight = Height;
  info.biPlanes = 1; // we only have one bitplane
  info.biBitCount = PixelDepth; // RGB mode is 24 bits
  info.biCompression = BI_RGB;
  info.biSizeImage = 0; // can be 0 for 24 bit images
  info.biXPelsPerMeter = 0x0ec4; // paint and PSP use this values
  info.biYPelsPerMeter = 0x0ec4;
  info.biClrUsed = 0; // we are in RGB mode and have no palette
  info.biClrImportant = 0; // all colors are importantenter code here
  </code></pre>
  
  <p>And I save it as follows:</p>
  
  <pre><code>    HANDLE file = CreateFile(bmpfile, GENERIC_WRITE, FILE_SHARE_READ,
      NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  if (file == NULL)
  {
      DWORD dw = GetLastError();
      CloseHandle(file);
  }
  
  // write file header
  if (WriteFile(file, &amp;bmfh, sizeof(BITMAPFILEHEADER), &amp;bwritten, NULL) == false)
  {
      DWORD dw = GetLastError();
      CloseHandle(file);
  }
  // write infoheader
  if (WriteFile(file, &amp;info, sizeof(BITMAPINFOHEADER), &amp;bwritten, NULL) == false)
  {
      DWORD dw = GetLastError();
      CloseHandle(file);
  }
  //write rgbquad for black
  if (WriteFile(file, &amp;rq, sizeof(rq), &amp;bwritten, NULL) == false)
  {
      DWORD dw = GetLastError();
      CloseHandle(file);
  }
  // write image data
  if (WriteFile(file, &amp;firstPixel[0], imageSize, &amp;bwritten, NULL) == false)
  {
      DWORD dw = GetLastError();
      CloseHandle(file);
  }
  
  // and clean up
  CloseHandle(file);
  </code></pre>
  
  <p>I think the above is the standard way of saving bitmap images. However, instead of saving the image, I want it to be available as BASE64 and pass it in a HTTP Post. Therefore, this question relates to <a href="https://stackoverflow.com/questions/19247396/saving-image-to-memory">this one</a>, but I am having a lot of difficulties converting the bmp structure to BASE64. I have taken the BASE64 encoder from <a href="https://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c">here</a>, but I have no idea how to pass the BMPFILEHEADER, BMPINFOHEADER, RGBQUAD, and raw data structure as a parameter to the BASE64 encoder. </p>
  
  <p>Any thoughts or pointers on how to combine the information I gathered?</p>
  
  <h1>UPDATE</h1>
  
  <p>Thanks to Roman Pustylnikov, I have gotten a bit farther already:
  I'm creating a struct like this:</p>
  
  <pre><code>    struct ImageBuffer
  {
      BITMAPFILEHEADER bfheader;
      BITMAPINFOHEADER infobmp;
      RGBQUAD rgb[256];
      PBYTE bitmap;
  };
  </code></pre>
  
  <p>Fill it as follows:</p>
  
  <pre><code>ImageBuffer capture;
  capture.bfheader = bmfh;
  capture.infobmp = info;
  // create the grayscale palette 
  for (int i = 0; i&lt;256; i++)
  {
      capture.rgb[i].rgbBlue = i;
      capture.rgb[i].rgbGreen = i;
      capture.rgb[i].rgbRed = i;
      capture.rgb[i].rgbReserved = 0;
  }
  capture.bitmap = firstPixel;
  </code></pre>
  
  <p>And convert it as follows:</p>
  
  <pre><code>int totalSize = sizeof(capture.bfheader) + sizeof(capture.infobmp) + sizeof(capture.rgb) + imageSize;
  std::string encodedImage = base64_encode(reinterpret_cast&lt;const unsigned char*&gt;(&amp;capture), totalSize);
  </code></pre>
  
  <p>However, it gives me an invalid bitmap. Also, when I load the bitmap from disk (the one that is generated with writefile), I get a different base64 string. I use C# code to compare the two Base64 strings:</p>
  
  <pre><code>            // generated base64 string
          string test = "Put base64string generated from C++ here";
          byte[] imageBytes = Convert.FromBase64String(test);
  
          // generate the same string based on the actual bmp
          byte[] data = File.ReadAllBytes(@"c:\successtest.bmp");
          string original = Convert.ToBase64String(data);
  </code></pre>
  
  <h1>UPDATE TWO: solution</h1>
  
  <p>The solution can be found in the latest update of Roman Pustylnikov's answer. </p>