我现在在WriteDIBList 函数中使用setDIBits 函数得到了一个HBITMAP对象,由于WriteDIBList 是全局函数,我不能看到效果,但我使用WriteDIB(把位图保存到文件中去),画不出图来,代码如下: // WriteDIB - Writes a DIB to file // Returns - TRUE on success // szFile - Name of file to write to // hDIB - Handle of the DIB BOOL WriteDIB( HGLOBAL hDIB) { BITMAPFILEHEADER hdr; LPBITMAPINFOHEADER lpbi; HANDLE hFile; DWORD dwBytesWritten; if (!hDIB) return FALSE; if(n == 0) { n = saveFileName.Replace("\\", "\\\\"); } // Open the existing file.
hFile = CreateFile( saveFileName, // open the BitMap file GENERIC_WRITE, //open for writing 0, // not share NULL, // no security CREATE_ALWAYS, // Creates a new file FILE_ATTRIBUTE_NORMAL, // normal file NULL); // no attr. template //fail to open file if (hFile == INVALID_HANDLE_VALUE) { return false; } lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB); if(!lpbi) { CloseHandle(hFile); return false; } 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 WriteFile(hFile, &hdr, sizeof(hdr), &dwBytesWritten, NULL);
// Write the DIB header and the bits WriteFile(hFile, lpbi, GlobalSize(hDIB), &dwBytesWritten, NULL);
GlobalUnlock(hDIB); CloseHandle(hFile); return TRUE; } //save the diblist to a Bmp file void writeDIBList(linklist L) { HGLOBAL hdib; LPVOID lpv; int startScan = 0; int nTotalLines = 0; int nScanLines; HDC hSrcdc, hMemDC; HBITMAP hbmp, holdbmp; hSrcdc = ::CreateDC("DISPLAY", NULL, NULL, NULL); hbmp = ::CreateCompatibleBitmap(hSrcdc, width, height + maxheight);
//nColors is the factual color num int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed : 1 << bmInfo.bmiHeader.biBitCount;
//16 bit or 24 bit or 32 bit clearly color if( bmInfo.bmiHeader.biBitCount > 8 ) lpv = (LPVOID)((LPDWORD)(bmInfo.bmiColors + bmInfo.bmiHeader.biClrUsed) + ((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
// WriteDIB - Writes a DIB to file
// Returns - TRUE on success
// szFile - Name of file to write to
// hDIB - Handle of the DIB
BOOL WriteDIB( HGLOBAL hDIB)
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;
HANDLE hFile;
DWORD dwBytesWritten; if (!hDIB)
return FALSE; if(n == 0)
{
n = saveFileName.Replace("\\", "\\\\");
} // Open the existing file.
hFile = CreateFile( saveFileName, // open the BitMap file
GENERIC_WRITE, //open for writing
0, // not share
NULL, // no security
CREATE_ALWAYS, // Creates a new file
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
//fail to open file
if (hFile == INVALID_HANDLE_VALUE)
{
return false;
} lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB); if(!lpbi)
{
CloseHandle(hFile);
return false;
} 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
WriteFile(hFile, &hdr, sizeof(hdr),
&dwBytesWritten, NULL);
// Write the DIB header and the bits
WriteFile(hFile, lpbi, GlobalSize(hDIB),
&dwBytesWritten, NULL);
GlobalUnlock(hDIB);
CloseHandle(hFile);
return TRUE;
}
//save the diblist to a Bmp file
void writeDIBList(linklist L)
{
HGLOBAL hdib;
LPVOID lpv;
int startScan = 0;
int nTotalLines = 0;
int nScanLines;
HDC hSrcdc, hMemDC;
HBITMAP hbmp, holdbmp; hSrcdc = ::CreateDC("DISPLAY", NULL, NULL, NULL);
hbmp = ::CreateCompatibleBitmap(hSrcdc, width, height + maxheight);
while (L != NULL)
{
if(L->hdib == NULL)
{
return;
} BITMAPINFO &bmInfo = *(LPBITMAPINFO)L->hdib ;
//nColors is the factual color num
int nColors = bmInfo.bmiHeader.biClrUsed ?
bmInfo.bmiHeader.biClrUsed :
1 << bmInfo.bmiHeader.biBitCount;
//16 bit or 24 bit or 32 bit clearly color
if( bmInfo.bmiHeader.biBitCount > 8 )
lpv = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
bmInfo.bmiHeader.biClrUsed) +
((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
//Other color
else
lpv = (LPVOID)(bmInfo.bmiColors + nColors); startScan = startScan + nTotalLines;
nScanLines = ::SetDIBits(hSrcdc,
hbmp,
startScan,
bmInfo.bmiHeader.biHeight,
lpv,
&bmInfo,
DIB_RGB_COLORS); nTotalLines = nTotalLines + nScanLines; L = L->next;
} hMemDC = ::CreateCompatibleDC(hSrcdc);
holdbmp = (HBITMAP)::SelectObject(hMemDC, hbmp);
//L = NULL; if(!::BitBlt(hMemDC, 0, 0, width, height + maxheight, hSrcdc, 0, 0, SRCCOPY))
return; hbmp = (HBITMAP)::SelectObject(hMemDC, holdbmp);
::DeleteDC(hSrcdc);
::DeleteDC(hMemDC); hdib = (HGLOBAL) hbmp;
WriteDIB(hdib);
}
还望众位大虾多多指教!
//save the diblist to a Bmp file
//save the diblist to a Bmp file
void writeDIBList(linklist L, CDC *pdc)
{
HGLOBAL hdib;
CDC memdc;
HBITMAP hbmp;
CRect rect(0, 0, width, height + maxheight);
memdc.CreateCompatibleDC(pdc);
DrawDIBList(rect, &memdc, L );//将保存在L中的位图画到memdc中去
hbmp = ::CreateCompatibleBitmap(memdc.m_hDC, width, height + maxheight);//获得memdc中位图的句柄
::DeleteDC(memdc);
hdib = BitmapToDIB(hbmp, NULL);//将位图转换为DIB格式
WriteDIB(hdib);//将位图写入文件}上叙代码可能是从内存中获得hbmp有问题,当我再次打开它时,没有显示,还望众位大虾能提点在下,为盼!