I have the following code which does 2 things:Return a scanned fingerprint as base64Store this print as bitmapWhile the code works most of the time, sometimes an invalid bitmap is saved (I can't open the file). I think there are some improvements (padding?) that can counter this strange behavior. The 'firstPixel' is a reference that is given by the Windows Biometric Framework, as well as the Width and the Height.PBYTE firstPixel = (PBYTE)((PBYTE)AnsiBdbRecord) + sizeof(WINBIO_BDB_ANSI_381_RECORD);// declare bmp structures//https://msdn.microsoft.com/en-us/library/windows/desktop/dd183391%28v=vs.85%29.aspx//Declare pbitmapinfo and initialize//PBITMAPINFO info;//info = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);//Declare fileheaderBITMAPFILEHEADER bmfh;BITMAPINFOHEADER bmih;RGBQUAD rq[256];for (int rgbitem = 0; rgbitem<256; rgbitem++){    rq[rgbitem].rgbBlue = BYTE(rgbitem);    rq[rgbitem].rgbGreen = BYTE(rgbitem);    rq[rgbitem].rgbRed = BYTE(rgbitem);    rq[rgbitem].rgbReserved = BYTE(0);}// and initialize them to zeromemset(&bmfh, 0, sizeof(BITMAPFILEHEADER));memset(&bmih, 0, sizeof(BITMAPINFOHEADER));//---------padding//calculate scanline int padding = 0;int scanlinebytes = Width;while ((scanlinebytes + padding) % 4 != 0)    padding++;//calculate new width, which will be used further instead of original Widthint paddedWidth = scanlinebytes + padding;//calculate new imageSizeint imageSize = paddedWidth * Height;//----------padding buffer//a new buffer is created to copy over the pixels from the firstPixel array//this is required for padding (to make it easier, we also copy over the original array in case no padding is required)PBYTE paddedPixel = new BYTE[imageSize];//set all pixels to zero, so we do not have to add padding manuallymemset(paddedPixel, 0, imageSize);long oldpos = 0;long newpos = 0;for (int y = 0; y < Height; y++){    for (int x = 0; x < Width; x++)    {        oldpos = y * Width + x;     // position in original buffer        newpos = y * paddedWidth + x;   // position in padded buffer        paddedPixel[newpos] = firstPixel[oldpos]; //fill padded buffer    }}//----------end of padding buffer//---------end of padding// fill the infoheaderbmih.biSize = sizeof(BITMAPINFOHEADER);bmih.biWidth = paddedWidth;bmih.biHeight = Height;bmih.biPlanes = 1; // we only have one bitplanebmih.biBitCount = PixelDepth; bmih.biCompression = BI_RGB;bmih.biSizeImage = 0; bmih.biXPelsPerMeter = 0x0ec4; // paint and PSP use this valuesbmih.biYPelsPerMeter = 0x0ec4;bmih.biClrUsed = 0; // we are in RGB mode and have no palettebmih.biClrImportant = 0; // all colors are important// fill the fileheader with databmfh.bfType = 0x4d42; // 0x4d42 = 'BM'bmfh.bfReserved1 = 0;bmfh.bfReserved2 = 0;bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+ 256 * sizeof(RGBQUAD) + imageSize;bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);//declare and initialize bitmapinfoheader based on bitmapinfoint totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256 + imageSize;//Put everything in a temporary buffer, and convert buffer to base64unsigned char * bmpBuff = new unsigned char[totalSize];int j = 0;memcpy((void *)(&(bmpBuff[j])), (void *)&bmfh, sizeof(BITMAPFILEHEADER));j += sizeof(BITMAPFILEHEADER);memcpy((void *)(&(bmpBuff[j])), (void *)&bmih, sizeof(BITMAPINFOHEADER));j += (sizeof(BITMAPINFOHEADER));memcpy((void *)(&(bmpBuff[j])), (void *)&rq, sizeof(RGBQUAD) * 256);j += sizeof(RGBQUAD) * 256;memcpy((void *)(&(bmpBuff[j])), (void *)&paddedPixel[0], imageSize);std::string encodedFile = base64_encode(bmpBuff, totalSize);delete bmpBuff;//Write file. unsigned long bwritten;// now we open the file to write toHANDLE 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 headerif (WriteFile(file, &bmfh, sizeof(BITMAPFILEHEADER), &bwritten, NULL) == false){    DWORD dw = GetLastError();    CloseHandle(file);}// write infoheaderif (WriteFile(file, &bmih, sizeof(BITMAPINFOHEADER), &bwritten, NULL) == false){    DWORD dw = GetLastError();    CloseHandle(file);}//write rgbquad for blackif (WriteFile(file, &rq, + sizeof(RGBQUAD) * 256, &bwritten, NULL) == false){    DWORD dw = GetLastError();    CloseHandle(file);}// write image dataif (WriteFile(file, &paddedPixel[0], imageSize, &bwritten, NULL) == false){    DWORD dw = GetLastError();    CloseHandle(file);}// and clean upCloseHandle(file);return encodedFile;}
Bitmap to base64 and writefile
c++;beginner;image;file