15,979
社区成员
发帖
与我相关
我的任务
分享
BOOL Save2BMPFile(char* FileName, char *RGBData, int RGBDataSize, int width, int height)
{
char *pszTmp = NULL;
char *pTmp = NULL;
HDC dc = CreateDC( TEXT("DISPLAY"), NULL, NULL, NULL );
HDC dc2 = CreateCompatibleDC( dc );
int iCountLine = 1;
long lLineHeight = 20;
long lWidthBytes, lNewHeight;
if(width%4 == 0)
lWidthBytes = width * 3;
else
lWidthBytes = (width+(4-width%4)) * 3;
lNewHeight = height+(iCountLine*lLineHeight);
// 创建新大小的图片内存
pszTmp = new char[lWidthBytes * lNewHeight];
memset( pszTmp, 120,lWidthBytes * lNewHeight);
memcpy(pszTmp + (lWidthBytes * iCountLine*lLineHeight),RGBData,lWidthBytes * height);
pTmp = new char[lWidthBytes];
int l;
for ( l = 0; l < lNewHeight / 2; l++ )
{
memcpy( pTmp, pszTmp + lWidthBytes * l, lWidthBytes );
memcpy( pszTmp + lWidthBytes * l, pszTmp + lWidthBytes * (lNewHeight - 1 - l), lWidthBytes );
memcpy( pszTmp + lWidthBytes * (lNewHeight - 1 - l), pTmp, lWidthBytes );
}
HBITMAP hBitmap = CreateBitmap( width, lNewHeight, 1, 24, pszTmp );
SelectObject( dc2, hBitmap );
l = 0;
TextOut( dc2,5,height, "abc", 3 ); // 在新增加的调试上写个
HBITMAP retBitMap = (HBITMAP)GetCurrentObject(dc2,OBJ_BITMAP);
long lGet = GetBitmapBits(retBitMap,lWidthBytes * lNewHeight,pszTmp);
for ( l = 0; l < lNewHeight / 2; l++ )
{
memcpy( pTmp, pszTmp + lWidthBytes * l, lWidthBytes );
memcpy( pszTmp + lWidthBytes * l, pszTmp + lWidthBytes * (lNewHeight - 1 - l), lWidthBytes );
memcpy( pszTmp + lWidthBytes * (lNewHeight - 1 - l), pTmp, lWidthBytes );
}
// 保存为文件
BITMAPFILEHEADER bmfHdr;
BITMAPINFOHEADER bmiHdr;
ZeroMemory( &bmfHdr, sizeof( bmfHdr ) );
ZeroMemory( &bmiHdr, sizeof( bmiHdr ) );
bmiHdr.biSize = sizeof(BITMAPINFOHEADER);
bmiHdr.biWidth = width;
bmiHdr.biHeight = lNewHeight;
bmiHdr.biPlanes = 1;
bmiHdr.biBitCount = 24;
bmiHdr.biCompression = BI_RGB;
bmiHdr.biSizeImage = 0;
bmiHdr.biXPelsPerMeter = 0;
bmiHdr.biYPelsPerMeter = 0;
bmiHdr.biClrUsed = 0;
bmiHdr.biClrImportant = 0;
bmfHdr.bfType = ((WORD) ('M' << 8) | 'B');
bmfHdr.bfSize=(DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) + lWidthBytes*lNewHeight);
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits=(DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER));
FILE *fp;
fp = fopen(FileName,"w+b");
if(fp != NULL)
{
fwrite((LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), 1, fp);
fwrite((LPSTR)&bmiHdr,sizeof(BITMAPINFOHEADER), 1, fp);
int size = lWidthBytes*lNewHeight;
fwrite( pszTmp, lWidthBytes*lNewHeight,1, fp );
fclose(fp);
return TRUE;
}
return FALSE;
}