RGB565格式图片存为16位或许24位BMP

RGB565格式图片存为16位或者24位BMP
有一个一维数组image1保存的是RGB565的图片数据,可以确保图片可以显示成功(已经实现),现在想将其保存为16位或者24位的BMP文件,但是我总是保存不对,图片颜色不对,并且变形了,代码如下,先谢了:

void CTakePhoto::OnBnClickedKeep()
{
CString fileName=TEXT("tupian");
CString filePath=TEXT("\\Storage Card\\");
int iWidth = WIDTH;//WIDTH,HEIGHT是image1图片的高和宽,我用了宏定义分别为205 169
int iHeight= HEIGHT;
//invert(image1);
const unsigned char * pBuffer=image1;

 SaveDIB2Bmp( fileNum,fileName ,filePath ,iWidth,iHeight, pBuffer);

 fileNum++;

}

//构建BMP文件信息头   
void CTakePhoto::ConstructBih(int nWidth, int  nHeight, BITMAPINFO& bih)
{
int widthStep = (((nWidth * 16) + 31) & (~31)) / 8 ; //为了使得每行像素为4的倍数
    
bih.bmiHeader.biSize          = sizeof (BITMAPINFOHEADER) ;
bih.bmiHeader.biWidth         = nWidth;
bih.bmiHeader.biHeight        = -nHeight;
bih.bmiHeader.biPlanes        = 1;
bih.bmiHeader.biBitCount      = 16;
bih.bmiHeader.biCompression   = BI_BITFIELDS;//BI_RGB仅有555格式
bih.bmiHeader.biClrUsed       = 0;
bih.bmiHeader.biClrImportant  = 0;
bih.bmiHeader.biSizeImage= widthStep*nHeight*2;  //这里应该是数组长度,这个与这个算法的结果相等
bih.bmiHeader.biXPelsPerMeter=0;  
bih.bmiHeader.biYPelsPerMeter=0;  
    bih.bmiColors[1].rgbBlue = 0xf800;
bih.bmiColors[1].rgbGreen = 0x07e0;
bih.bmiColors[1].rgbRed = 0x001F;
bih.bmiColors[1].rgbReserved = 0; 

}

void CTakePhoto::ContructBhh(int nWidth, int nHeight, BITMAPFILEHEADER& bhh)
{
int widthStep = (((nWidth * 16) + 31) & (~31)) / 8 ; //每行实际占用的大小(每行都被填充到一个4字节边界)   

bhh.bfType = ((WORD) ('M' << 8) | 'B');  //'BM'    BITMAPINFOHEADER
bhh.bfSize = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFO) + widthStep*nHeight*2;
bhh.bfReserved1 = 0;  
bhh.bfReserved2 = 0;  
bhh.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFO);  
}



//保存buffer到bmp文件   

//iWidth:图像宽; iHeight:图像高;pBuffer:图像RGB数据;filePath:存储路径;fileName:保存文件名;