Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to print color BITMAP from ressource on Printer ?

How to print color BITMAP from ressource on Printer ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiographicshelptutorial
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jerome_data

    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:

    M Offline
    M Offline
    Mark Salsbery
    wrote on last edited by
    #2

    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")

    J 1 Reply Last reply
    0
    • J jerome_data

      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:

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #3

      You said createcompatiblebitmap has a problem but where you used it? and whats error?


      WhiteSky


      J 1 Reply Last reply
      0
      • H Hamid Taebi

        You said createcompatiblebitmap has a problem but where you used it? and whats error?


        WhiteSky


        J Offline
        J Offline
        jerome_data
        wrote on last edited by
        #4

        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); } }

        P H 2 Replies Last reply
        0
        • M Mark Salsbery

          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")

          J Offline
          J Offline
          jerome_data
          wrote on last edited by
          #5

          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); ... }

          H 1 Reply Last reply
          0
          • J jerome_data

            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); } }

            P Offline
            P Offline
            prasad_som
            wrote on last edited by
            #6

            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[][^]

            M 1 Reply Last reply
            0
            • J jerome_data

              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); } }

              H Offline
              H Offline
              Hamid Taebi
              wrote on last edited by
              #7

              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


              1 Reply Last reply
              0
              • J jerome_data

                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); ... }

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #8

                I think your maybe your problem is of bmp.Load();(your answer to my reply)


                WhiteSky


                1 Reply Last reply
                0
                • P prasad_som

                  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[][^]

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #9

                  Good eye :)

                  "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

                  P 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    Good eye :)

                    "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

                    P Offline
                    P Offline
                    prasad_som
                    wrote on last edited by
                    #10

                    Thanks ! But, seems like OP is not interested to reply back, once his problem is known.

                    Prasad Notifier using ATL | Operator new[],delete[][^]

                    1 Reply Last reply
                    0
                    • J jerome_data

                      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:

                      J Offline
                      J Offline
                      jerome_data
                      wrote on last edited by
                      #11

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups