......
StartDoc(hDC,&doc);
StartPage(hDC);
TextOut(hDC, 0, 0, "Hello", 5);
DC2BMP(hDC, "test.bmp");
EndPage(hDC);
EndDoc(hDC);
...... if DC2BMP Function is called with ScreenDC, BMP file is OK.
if DC2BMP Function is called with PrinterDC, BMP file is not OK. BMP file is
whole white...
What's the problem? DC2BMP(hDC, "test.bmp")如下:HANDLE CBitMapTestDlg::DDBToDIB(CBitmap& bitmap, DWORD dwCompression,
CPalette* pPal) {
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDIB;
HANDLE handle;
HDC hDC;
HPALETTE hPal; ASSERT(bitmap.GetSafeHandle()); // The function has no arg for bitfields
if(dwCompression == BI_BITFIELDS)
return NULL; // If a palette has not been supplied use defaul palette
if((hPal = (HPALETTE) pPal->GetSafeHandle()) == NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE); // Get bitmap information
bitmap.GetObject(sizeof(bm),(LPSTR)&bm); // Initialize the bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0; // Compute the size of the infoheader and the color table
int nColors = (1 << bi.biBitCount);
if(nColors > 256)
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD);
// We need a device context to get the DIB from
hDC = ::GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC);
// Allocate enough memory to hold bitmapinfoheader and color table
if(!(hDIB = GlobalAlloc(GMEM_FIXED,dwLen))) {
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)hDIB;
*lpbi = bi; // Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
bi = *lpbi; // If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if(bi.biSizeImage == 0) {
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) *
bi.biHeight;
// If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if(dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
}
// Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
if(handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
hDIB = handle;
else {
GlobalFree(hDIB);
// Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}
// Get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDIB; // FINALLY get the DIB
BOOL bGotBits = GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L,
(DWORD)bi.biHeight, (LPBYTE)lpbi + (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS); // Use RGB for color table
if(!bGotBits) {
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return hDIB;
} // WriteDIB - Writes a DIB to file
// Returns - TRUE on success
// szFile - Name of file to write to
// hDIB - Handle of the DIB
BOOL CBitMapTestDlg::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 = DWORD(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, (UINT)GlobalSize(hDIB));
return TRUE;
} void CBitMapTestDlg::DC2BMP(HDC hdc, char* filename) {
CDC srcDC, memDC;
CBitmap bitmap, *pbitmap=NULL;
srcDC.Attach(hdc);
memDC.CreateCompatibleDC(&srcDC);
bitmap.CreateCompatibleBitmap(&srcDC, srcDC.GetDeviceCaps(HORZRES),
srcDC.GetDeviceCaps(VERTRES));
pbitmap = memDC.SelectObject(&bitmap);
memDC.BitBlt(0, 0, srcDC.GetDeviceCaps(HORZRES),
srcDC.GetDeviceCaps(VERTRES), &srcDC, 0, 0, SRCCOPY);
HANDLE hDIB = DDBToDIB(bitmap, BI_RGB, NULL);
WriteDIB(filename, hDIB);
srcDC.Detach();
}
StartDoc(hDC,&doc);
StartPage(hDC);
TextOut(hDC, 0, 0, "Hello", 5);
DC2BMP(hDC, "test.bmp");
EndPage(hDC);
EndDoc(hDC);
...... if DC2BMP Function is called with ScreenDC, BMP file is OK.
if DC2BMP Function is called with PrinterDC, BMP file is not OK. BMP file is
whole white...
What's the problem? DC2BMP(hDC, "test.bmp")如下:HANDLE CBitMapTestDlg::DDBToDIB(CBitmap& bitmap, DWORD dwCompression,
CPalette* pPal) {
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDIB;
HANDLE handle;
HDC hDC;
HPALETTE hPal; ASSERT(bitmap.GetSafeHandle()); // The function has no arg for bitfields
if(dwCompression == BI_BITFIELDS)
return NULL; // If a palette has not been supplied use defaul palette
if((hPal = (HPALETTE) pPal->GetSafeHandle()) == NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE); // Get bitmap information
bitmap.GetObject(sizeof(bm),(LPSTR)&bm); // Initialize the bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0; // Compute the size of the infoheader and the color table
int nColors = (1 << bi.biBitCount);
if(nColors > 256)
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD);
// We need a device context to get the DIB from
hDC = ::GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC);
// Allocate enough memory to hold bitmapinfoheader and color table
if(!(hDIB = GlobalAlloc(GMEM_FIXED,dwLen))) {
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)hDIB;
*lpbi = bi; // Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
bi = *lpbi; // If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if(bi.biSizeImage == 0) {
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) *
bi.biHeight;
// If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if(dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
}
// Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
if(handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
hDIB = handle;
else {
GlobalFree(hDIB);
// Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}
// Get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDIB; // FINALLY get the DIB
BOOL bGotBits = GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L,
(DWORD)bi.biHeight, (LPBYTE)lpbi + (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS); // Use RGB for color table
if(!bGotBits) {
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return hDIB;
} // WriteDIB - Writes a DIB to file
// Returns - TRUE on success
// szFile - Name of file to write to
// hDIB - Handle of the DIB
BOOL CBitMapTestDlg::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 = DWORD(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, (UINT)GlobalSize(hDIB));
return TRUE;
} void CBitMapTestDlg::DC2BMP(HDC hdc, char* filename) {
CDC srcDC, memDC;
CBitmap bitmap, *pbitmap=NULL;
srcDC.Attach(hdc);
memDC.CreateCompatibleDC(&srcDC);
bitmap.CreateCompatibleBitmap(&srcDC, srcDC.GetDeviceCaps(HORZRES),
srcDC.GetDeviceCaps(VERTRES));
pbitmap = memDC.SelectObject(&bitmap);
memDC.BitBlt(0, 0, srcDC.GetDeviceCaps(HORZRES),
srcDC.GetDeviceCaps(VERTRES), &srcDC, 0, 0, SRCCOPY);
HANDLE hDIB = DDBToDIB(bitmap, BI_RGB, NULL);
WriteDIB(filename, hDIB);
srcDC.Detach();
}
解决方案 »
- 关于孙鑫老师的视频 第15课中最后一个简单的聊天程序中的一个问题
- 文件写入问题············
- 用QT做界面好么?还有没别的办法?
- 类的设计问题,高内聚低耦合 怎么处理 谢谢
- 频繁开关线程对系统性能和开销的影响有多大?
- MessageBox與::MessageBox有什麼區別
- 关于TAPI的事@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- 关于Windows下程序异常问题--在线等待!!
- 救命啊
- 有熟悉ERP的请帮忙
- MFC 如何实现Photoshop中的带衰减度(硬度)的线条
- 各位高手,我用VC6.0派生个ClistCtrl类,为什么LVN_ENDSCROLL不响应
对GDI的调用,都传给了打印驱动,在内存并没有相应内存数据.
你只有也虚拟打印机,才能获得对应的图片.
往打印DC作图时, 对GDI的调用会全部转换成打印驱动API的调用, 你能否得到图像依赖于驱动的实现.通常我们的做法时, 写个虚拟打印机(实现打印驱动), 输出的图像数据转存在文件.
至于实现虚拟打印机, 不是一两句话就说得清楚, 请在网上查找相关资料.