How to print color BITMAP from ressource on Printer ?
-
I use MFC SDI in vs 2005. I have a problem to print bitmap on my printer. It's works on OnFilePrintPreview() but not on the OnFilePrint() I try with createcompatiblebitmap but i have a crash. My code: My printer is selected here, i have a pDC with m_hDC unused. void MyView::OnPrint(CDC *pDC, CPrintInfo *pInfo) { CBitmap bmp; if (bmp.LoadBitmap(IDB_BITMAP_1)) { BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); CDC dcMemory; BOOL result; result=dcMemory.CreateCompatibleDC(pDC); CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp); result=pDC->BitBlt(x, y, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 0, 0, SRCCOPY ); dcMemory.SelectObject(pOldBitmap); } } thanks :doh:
What do you mean by crash? Assertion error? BSOD? Is pDC valid or NULL?
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")
-
I use MFC SDI in vs 2005. I have a problem to print bitmap on my printer. It's works on OnFilePrintPreview() but not on the OnFilePrint() I try with createcompatiblebitmap but i have a crash. My code: My printer is selected here, i have a pDC with m_hDC unused. void MyView::OnPrint(CDC *pDC, CPrintInfo *pInfo) { CBitmap bmp; if (bmp.LoadBitmap(IDB_BITMAP_1)) { BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); CDC dcMemory; BOOL result; result=dcMemory.CreateCompatibleDC(pDC); CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp); result=pDC->BitBlt(x, y, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 0, 0, SRCCOPY ); dcMemory.SelectObject(pOldBitmap); } } thanks :doh:
You said createcompatiblebitmap has a problem but where you used it? and whats error?
WhiteSky
-
You said createcompatiblebitmap has a problem but where you used it? and whats error?
WhiteSky
the error come from ASSERT on the handle of the pDC in this function: BOOL CGdiObject::Attach(HGDIOBJ hObject) { ASSERT(m_hObject == NULL); ... } i try CreateCompatibleBitmap : void MyView::OnPrint(CDC *pDC, CPrintInfo *pInfo) { CBitmap bmp; if (bmp.LoadBitmap(IDB_BITMAP_1)) { BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); CDC dcMemory; BOOL result; result=dcMemory.CreateCompatibleDC(pDC); CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp); bmp.CreateCompatibleBitmap(&dcMemory,bmpInfo.bmWidth,bmpInfo.bmHeight); result=pDC->BitBlt(x, y, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 0, 0, SRCCOPY ); dcMemory.SelectObject(pOldBitmap); } }
-
What do you mean by crash? Assertion error? BSOD? Is pDC valid or NULL?
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")
the pDC is valid because print text and line works correctly, i have only a assert error on: ... dcMemory.CreateCompatibleDC(pDC); bmp.CreateCompatibleBitmap(&dcMemory,bmpInfo.bmWidth,bmpInfo.bmHeight); ... but the handle of pDC is unused i have assert error here when i try CreateCompatibleBitmap(ASSERT on the dcMemory): BOOL CGdiObject::Attach(HGDIOBJ hObject) { ASSERT(m_hObject == NULL); ... }
-
the error come from ASSERT on the handle of the pDC in this function: BOOL CGdiObject::Attach(HGDIOBJ hObject) { ASSERT(m_hObject == NULL); ... } i try CreateCompatibleBitmap : void MyView::OnPrint(CDC *pDC, CPrintInfo *pInfo) { CBitmap bmp; if (bmp.LoadBitmap(IDB_BITMAP_1)) { BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); CDC dcMemory; BOOL result; result=dcMemory.CreateCompatibleDC(pDC); CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp); bmp.CreateCompatibleBitmap(&dcMemory,bmpInfo.bmWidth,bmpInfo.bmHeight); result=pDC->BitBlt(x, y, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 0, 0, SRCCOPY ); dcMemory.SelectObject(pOldBitmap); } }
jerome_data wrote:
bmp.CreateCompatibleBitmap(&dcMemory,bmpInfo.bmWidth,bmpInfo.bmHeight);
CreateCompatibleBitmap
initializes a bitmap compatible to mentioned DC. In your case, its already initialized. Thats why assert is there. Comment this line, as it is not required.Prasad Notifier using ATL | Operator new[],delete[][^]
-
the error come from ASSERT on the handle of the pDC in this function: BOOL CGdiObject::Attach(HGDIOBJ hObject) { ASSERT(m_hObject == NULL); ... } i try CreateCompatibleBitmap : void MyView::OnPrint(CDC *pDC, CPrintInfo *pInfo) { CBitmap bmp; if (bmp.LoadBitmap(IDB_BITMAP_1)) { BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); CDC dcMemory; BOOL result; result=dcMemory.CreateCompatibleDC(pDC); CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp); bmp.CreateCompatibleBitmap(&dcMemory,bmpInfo.bmWidth,bmpInfo.bmHeight); result=pDC->BitBlt(x, y, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 0, 0, SRCCOPY ); dcMemory.SelectObject(pOldBitmap); } }
You assign IDB_BITMAP_1 to bmp object and then create a another bitmap you can before CreateCompatibleBitmap use of bmp.detach() and create your bitmap
WhiteSky
-
the pDC is valid because print text and line works correctly, i have only a assert error on: ... dcMemory.CreateCompatibleDC(pDC); bmp.CreateCompatibleBitmap(&dcMemory,bmpInfo.bmWidth,bmpInfo.bmHeight); ... but the handle of pDC is unused i have assert error here when i try CreateCompatibleBitmap(ASSERT on the dcMemory): BOOL CGdiObject::Attach(HGDIOBJ hObject) { ASSERT(m_hObject == NULL); ... }
I think your maybe your problem is of bmp.Load();(your answer to my reply)
WhiteSky
-
jerome_data wrote:
bmp.CreateCompatibleBitmap(&dcMemory,bmpInfo.bmWidth,bmpInfo.bmHeight);
CreateCompatibleBitmap
initializes a bitmap compatible to mentioned DC. In your case, its already initialized. Thats why assert is there. Comment this line, as it is not required.Prasad Notifier using ATL | Operator new[],delete[][^]
Good eye :)
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")
-
Good eye :)
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")
Thanks ! But, seems like
OP
is not interested to reply back, once his problem is known.Prasad Notifier using ATL | Operator new[],delete[][^]
-
I use MFC SDI in vs 2005. I have a problem to print bitmap on my printer. It's works on OnFilePrintPreview() but not on the OnFilePrint() I try with createcompatiblebitmap but i have a crash. My code: My printer is selected here, i have a pDC with m_hDC unused. void MyView::OnPrint(CDC *pDC, CPrintInfo *pInfo) { CBitmap bmp; if (bmp.LoadBitmap(IDB_BITMAP_1)) { BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); CDC dcMemory; BOOL result; result=dcMemory.CreateCompatibleDC(pDC); CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp); result=pDC->BitBlt(x, y, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 0, 0, SRCCOPY ); dcMemory.SelectObject(pOldBitmap); } } thanks :doh:
Used this code to print a bitmap on printer and display void YourClass::PrintBitmap(CDC *pDC, CPrintInfo *pInfo,UINT Bitmap,int x,int y) { LPBITMAPINFO info; // Structure for storing the DIB information, // it will be used by 'StretchDIBits()' HBITMAP hbit; // Handle to the bitmap to print BITMAP bm; // Structure used for obtaining information // about the bitmap (size, color depth...) int nColors = 0; // Used to store the number of colors the DIB has int sizeinfo = 0; // Will contain the size of info RGBQUAD rgb[256]; // Used to store the DIB color table // The following line loads the bitmap from resource bitmap hbit = (HBITMAP) LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(Bitmap), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION); // Obtain information about 'hbit' and store it in 'bm' GetObject(hbit, sizeof(BITMAP), (LPVOID) &bm); nColors = (1 << bm.bmBitsPixel); if(nColors > 256) nColors=0; // This is when DIB is 24 bit. // In this case there is not any color table information // Now we need to know how much size we have to give for storing "info" in memory. // This involves the proper BITMAPINFO size and the color table size. // Color table is only needed when the DIB has 256 colors or less. sizeinfo = sizeof(BITMAPINFO) + (nColors * sizeof(RGBQUAD)); // This is the size required info = (LPBITMAPINFO) malloc(sizeinfo); // Storing info in memory // Before 'StretchDIBits()' we have to fill some "info" fields. // This information was stored in 'bm'. info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); info->bmiHeader.biWidth = bm.bmWidth; info->bmiHeader.biHeight = bm.bmHeight; info->bmiHeader.biPlanes = 1; info->bmiHeader.biBitCount = bm.bmBitsPixel * bm.bmPlanes; info->bmiHeader.biCompression = BI_RGB; info->bmiHeader.biSizeImage = bm.bmWidthBytes * bm.bmHeight; info->bmiHeader.biXPelsPerMeter = 0; info->bmiHeader.biYPelsPerMeter = 0; info->bmiHeader.biClrU