int GetDIBits( HDC hdc, // handle to DC HBITMAP hbmp, // handle to bitmap UINT uStartScan, // first scan line to set UINT cScanLines, // number of scan lines to copy LPVOID lpvBits, // array for bitmap bits LPBITMAPINFO lpbi, // bitmap data buffer UINT uUsage // RGB or palette index );
如果具有一个设备无关的位图句柄,把一个位图写入BMP文件非常简单:在位图内容之后写入BITMAPINFOHEADER信息即可。需要设置BITMAPINFOHEADER的三个成员是bfType,其值为"BM",bfSize,其值是位图的大小,以及,bfOffBits,其值为文件开始到位图位的偏移量。 // WriteDIB - Writes a DIB to file
// Returns - TRUE on success
// szFile - Name of file to write to
// hDIB - Handle of the DIB
BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB)
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi; if (!hDIB)
return FALSE; CFile file;
if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
return FALSE; lpbi = (LPBITMAPINFOHEADER)hDIB; int nColors = 1 << lpbi->biBitCount; // Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM" hdr.bfSize="GlobalSize" (hDIB) + sizeof( hdr ); hdr.bfReserved1="0;" hdr.bfReserved2="0;" hdr.bfOffBits="(DWORD)" (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD)); // Write the file header
file.Write( &hdr, sizeof(hdr) ); // Write the DIB header and the bits
file.Write( lpbi, GlobalSize(hDIB) ); return TRUE;
}
void CCreateRandomBMPDlg::OnBtnCreateBMP()
{
CDC dc;
dc.CreateDC("DISPLAY", NULL, NULL, NULL);
CBitmap bm;
int Width = 800;//GetSystemMetrics(SM_CXSCREEN);
int Height = 600;//GetSystemMetrics(SM_CYSCREEN);
bm.CreateCompatibleBitmap(&dc, Width, Height);
CDC tdc;
tdc.CreateCompatibleDC(&dc);
CBitmap* pOld = tdc.SelectObject(&bm);
tdc.BitBlt(0, 0, Width, Height, &dc, 0, 0, SRCCOPY);
tdc.SelectObject(pOld); BITMAP btm;
bm.GetBitmap(&btm);
DWORD size = btm.bmWidthBytes* btm.bmHeight;
LPSTR lpData = (LPSTR) GlobalAllocPtr(GPTR, size);
/////////////////////////////////////////////
/////////////////////////////////////////////
BITMAPINFOHEADER bih;
bih.biBitCount = btm.bmBitsPixel;
bih.biClrImportant = 0;
bih.biClrUsed = 0;
bih.biCompression = 0;
bih.biHeight = btm.bmHeight;
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = size;
bih.biWidth = btm.bmWidth;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
///////////////////////////////////
GetDIBits(dc, bm, 0, bih.biHeight, lpData, (BITMAPINFO *) &bih,
DIB_RGB_COLORS);
// bm.GetBitmapBits(size,lpData); //此函数在处理5-5-5模式的16位色下会出现颜色混乱
//////////////////////////////
//修改RGB值
int nWidth = btm.bmWidth * 4;
for (int i = 0; i < btm.bmHeight; i++)
{
for (int j = 0; j < btm.bmWidth; j++)
{
lpData[i * nWidth + j * 4 + 2] = GetRandomRGBValue(); //R
lpData[i * nWidth + j * 4 + 1] = GetRandomRGBValue(); //G
lpData[i * nWidth + j * 4] = GetRandomRGBValue(); //B
TRACE("\nR = %d; G = %d; B = %d\n",
lpData[i * nWidth + j * 4 + 2],
lpData[i * nWidth + j * 4 + 1], lpData[i * nWidth + j * 4]);
}
} static int filecount = 0;
CString name;
name = "D:\\Test.bmp";//m_Path+name;
BITMAPFILEHEADER bfh;
bfh.bfReserved1 = bfh.bfReserved2 = 0;
bfh.bfType = ((WORD) ('M' << 8) | 'B');
bfh.bfSize = 54 + size;
bfh.bfOffBits = 54; CFile bf;
if (bf.Open(name, CFile::modeCreate | CFile::modeWrite))
{
bf.WriteHuge(&bfh, sizeof(BITMAPFILEHEADER));
bf.WriteHuge(&bih, sizeof(BITMAPINFOHEADER));
bf.WriteHuge(lpData, size);
bf.Close();
}
GlobalFreePtr(lpData);
AfxMessageBox("Create BMP File Over!");
}
HDC hdc, // handle to DC
HBITMAP hbmp, // handle to bitmap
UINT uStartScan, // first scan line to set
UINT cScanLines, // number of scan lines to copy
LPVOID lpvBits, // array for bitmap bits
LPBITMAPINFO lpbi, // bitmap data buffer
UINT uUsage // RGB or palette index
);