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 can I print a bitmap file ?

How can I print a bitmap file ?

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsworkspace
14 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.
  • C Christian Graus

    A printer has a DC, you simply StretchBlt it from a DC containing your bitmap to the printer. You will *always* stretch it, because otherwise the image will be smaller than a postage stamp. Don't forget to set the COLORONCOLOR StretchBlt mode. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

    Sonork ID 100.10002:MeanManOz

    I live in Bob's HungOut now

    C Offline
    C Offline
    Chambers
    wrote on last edited by
    #3

    Christian, can you explain your answer a little bit more, cos I have had this problem on my app. I managed to fix it by stretchblt'ing it with an offset, however this only worked for my PC. I recently tested it on someone else's PC and it was like a postage stamp despite my offset. Again, ur input would be much appreciated, Alan. P.S. did u fix that problem you had with the min/max track size of the child window (where it wouldn`t resize below a 100?) cos I posted a reply, but I think u must've solved it by that time. "When I left you I was but the learner, now I am the Master" - Darth Vader:mad:

    C 1 Reply Last reply
    0
    • C Chambers

      Christian, can you explain your answer a little bit more, cos I have had this problem on my app. I managed to fix it by stretchblt'ing it with an offset, however this only worked for my PC. I recently tested it on someone else's PC and it was like a postage stamp despite my offset. Again, ur input would be much appreciated, Alan. P.S. did u fix that problem you had with the min/max track size of the child window (where it wouldn`t resize below a 100?) cos I posted a reply, but I think u must've solved it by that time. "When I left you I was but the learner, now I am the Master" - Darth Vader:mad:

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #4

      You actually need to find out the size of the print DC for the specific printer you are working with. Also, some HP printers do not report this correctly if you're using DirectX, we spoke to HP about this and they said they know about the problem, but they will only fix it *in our app* ( they admit the problem is their drivers ) if we give them all our source code and a pile of money. I do it like this:

      CPrintDialog dlg(false);
      dlg.GetDefaults();
      
      HDC hdc = dlg.GetPrinterDC();
      ASSERT(hdc);
      
      CDC DC;
      DC.Attach(hdc);
      
          m\_PrintAreaHorz = GetDeviceCaps(hdc, HORZRES);
          m\_PrintAreaVert = GetDeviceCaps(hdc, VERTRES);
      
      DC.DeleteDC();
      

      which gives me the width and height I need to stretch to in order to fill the page. This should work for any printer, that is, if you stretch to these co-ordinates ( you'll obviously not want to, for aspect ratio reasons as much as anything ), you will fill the printable area of the page on any size/DPI printer. ****Chambers wrote: P.S. did u fix that problem you had with the min/max track size of the child window (where it wouldn`t resize below a 100?) cos I posted a reply, but I think u must've solved it by that time. No, I've left it for now. For some reasons the MinMax is right, but Windows is not letting me size down to a width of 50, it stops at 100. I think the problem is in our Python code, somewhere. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

      Sonork ID 100.10002:MeanManOz

      I live in Bob's HungOut now

      C M 2 Replies Last reply
      0
      • C Christian Graus

        You actually need to find out the size of the print DC for the specific printer you are working with. Also, some HP printers do not report this correctly if you're using DirectX, we spoke to HP about this and they said they know about the problem, but they will only fix it *in our app* ( they admit the problem is their drivers ) if we give them all our source code and a pile of money. I do it like this:

        CPrintDialog dlg(false);
        dlg.GetDefaults();
        
        HDC hdc = dlg.GetPrinterDC();
        ASSERT(hdc);
        
        CDC DC;
        DC.Attach(hdc);
        
            m\_PrintAreaHorz = GetDeviceCaps(hdc, HORZRES);
            m\_PrintAreaVert = GetDeviceCaps(hdc, VERTRES);
        
        DC.DeleteDC();
        

        which gives me the width and height I need to stretch to in order to fill the page. This should work for any printer, that is, if you stretch to these co-ordinates ( you'll obviously not want to, for aspect ratio reasons as much as anything ), you will fill the printable area of the page on any size/DPI printer. ****Chambers wrote: P.S. did u fix that problem you had with the min/max track size of the child window (where it wouldn`t resize below a 100?) cos I posted a reply, but I think u must've solved it by that time. No, I've left it for now. For some reasons the MinMax is right, but Windows is not letting me size down to a width of 50, it stops at 100. I think the problem is in our Python code, somewhere. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

        Sonork ID 100.10002:MeanManOz

        I live in Bob's HungOut now

        C Offline
        C Offline
        Chambers
        wrote on last edited by
        #5

        Thanks Christian, I`ve managed to get it going now, just hope it works on other PC's, wish me luck, Many Thanks, Alan. "When I left you I was but the learner, now I am the Master" - Darth Vader:mad:

        C 1 Reply Last reply
        0
        • C Chambers

          Thanks Christian, I`ve managed to get it going now, just hope it works on other PC's, wish me luck, Many Thanks, Alan. "When I left you I was but the learner, now I am the Master" - Darth Vader:mad:

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #6

          Good luck :-) Printing is a royal pain at times, but the way I showed you is the right way to get the dimensions for every page. I did three implimentations of printing in different apps before I felt I was doing it *well*. I'm not especially keen on how MFC puts it all together, either. _"Only a master of evil, Darth"_Obi Wan Kenobi Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

          Sonork ID 100.10002:MeanManOz

          I live in Bob's HungOut now

          1 Reply Last reply
          0
          • C Christian Graus

            You actually need to find out the size of the print DC for the specific printer you are working with. Also, some HP printers do not report this correctly if you're using DirectX, we spoke to HP about this and they said they know about the problem, but they will only fix it *in our app* ( they admit the problem is their drivers ) if we give them all our source code and a pile of money. I do it like this:

            CPrintDialog dlg(false);
            dlg.GetDefaults();
            
            HDC hdc = dlg.GetPrinterDC();
            ASSERT(hdc);
            
            CDC DC;
            DC.Attach(hdc);
            
                m\_PrintAreaHorz = GetDeviceCaps(hdc, HORZRES);
                m\_PrintAreaVert = GetDeviceCaps(hdc, VERTRES);
            
            DC.DeleteDC();
            

            which gives me the width and height I need to stretch to in order to fill the page. This should work for any printer, that is, if you stretch to these co-ordinates ( you'll obviously not want to, for aspect ratio reasons as much as anything ), you will fill the printable area of the page on any size/DPI printer. ****Chambers wrote: P.S. did u fix that problem you had with the min/max track size of the child window (where it wouldn`t resize below a 100?) cos I posted a reply, but I think u must've solved it by that time. No, I've left it for now. For some reasons the MinMax is right, but Windows is not letting me size down to a width of 50, it stops at 100. I think the problem is in our Python code, somewhere. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

            Sonork ID 100.10002:MeanManOz

            I live in Bob's HungOut now

            M Offline
            M Offline
            manio
            wrote on last edited by
            #7

            CPrintDialog dlg(false); dlg.GetDefaults(); HDC hdc = dlg.GetPrinterDC(); ASSERT(hdc); CDC DC; DC.Attach(hdc); m_PrintAreaHorz = GetDeviceCaps(hdc, HORZRES); m_PrintAreaVert = GetDeviceCaps(hdc, VERTRES); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ DC.DeleteDC(); Christian, Thanks for your reply. But I got another question as quoted as "^^^^^" above. Does this print to the "original size" of this bitmap file ? (i.e, original width and height of this bitmap):)

            C 1 Reply Last reply
            0
            • M manio

              CPrintDialog dlg(false); dlg.GetDefaults(); HDC hdc = dlg.GetPrinterDC(); ASSERT(hdc); CDC DC; DC.Attach(hdc); m_PrintAreaHorz = GetDeviceCaps(hdc, HORZRES); m_PrintAreaVert = GetDeviceCaps(hdc, VERTRES); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ DC.DeleteDC(); Christian, Thanks for your reply. But I got another question as quoted as "^^^^^" above. Does this print to the "original size" of this bitmap file ? (i.e, original width and height of this bitmap):)

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #8

              Well, it's given you the width and height of the print DC, it's got nothing to do with the bitmap. You'll need to know the physical size of the paper ( A3, A4, Legal) in order to turn that into something physical and then you need to use these values with respect to aspect ratio correction as well. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

              Sonork ID 100.10002:MeanManOz

              I live in Bob's HungOut now

              L 1 Reply Last reply
              0
              • C Christian Graus

                Well, it's given you the width and height of the print DC, it's got nothing to do with the bitmap. You'll need to know the physical size of the paper ( A3, A4, Legal) in order to turn that into something physical and then you need to use these values with respect to aspect ratio correction as well. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

                Sonork ID 100.10002:MeanManOz

                I live in Bob's HungOut now

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #9

                And, another question... If, what I get is just a buffer like RGB-24 data ? How to print it ? Do I need to convert or save it as a standard BMP format ? How to do that ? (i.e, save RGB-24 data to BMP format ?) What kind of header do I need to add ? Or how can I get the dimension (i.e, width and height) from this RGB-24 data ? Any reply would be greatly appreciated..

                C 1 Reply Last reply
                0
                • L Lost User

                  And, another question... If, what I get is just a buffer like RGB-24 data ? How to print it ? Do I need to convert or save it as a standard BMP format ? How to do that ? (i.e, save RGB-24 data to BMP format ?) What kind of header do I need to add ? Or how can I get the dimension (i.e, width and height) from this RGB-24 data ? Any reply would be greatly appreciated..

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #10

                  You cna pass the buffer into a CBitmap constructor, although I'm not sure if a printer DC will print any bit depth DDB. I'd play it safe and create a DIBSection, then you can just memset the buffer into it. If you call CreateDIBSection, you get a pointer to the data and you can copy your buffer into it. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

                  Sonork ID 100.10002:MeanManOz

                  I live in Bob's HungOut now

                  M 1 Reply Last reply
                  0
                  • C Christian Graus

                    A printer has a DC, you simply StretchBlt it from a DC containing your bitmap to the printer. You will *always* stretch it, because otherwise the image will be smaller than a postage stamp. Don't forget to set the COLORONCOLOR StretchBlt mode. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

                    Sonork ID 100.10002:MeanManOz

                    I live in Bob's HungOut now

                    M Offline
                    M Offline
                    manio
                    wrote on last edited by
                    #11

                    Christian, One more question about StretchBlt.. suppose my bitmap data to be printed is in some buffer like this: pCurrentImage, (i.e, as the following class CRGBImage { public: BYTE* data; int m_nWidth; // in pixel int m_nHeight; }; Now I already get something like this: CRGBImage* pCurrentImage... Then I want to print this image data, so I wrote the following codes. CPrintDialog dlg( FALSE ); CDC dc; dc.Attach( dlg.GetPrinterDC() ); //calculate printing area(width and height) int pwidth = dc.GetDeviceCaps(HORZRES);//get width of printing area int pheight = dc.GetDeviceCaps(VERTRES);//get height of printing area Now, how can I copy my bitmap data to dc ? And, for the following StretchBlt API: BOOL CDC::StretchBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, DWORD dwRop ); (Take the width for example) The above parameter nSrcWidth = pCurrentImage->m_nWidth, right ? And, how about the nWidth ? How to calculate it if I want to print to the original dimension of my bitmap ? How to calculate pSrcDC ? (Do I just use: dc.StretchBlt(...) as dc mentioned above ? Because there are 2 DCs, I got kind of confused !! ) And, when to put "COLORONCOLOR StretchBlt mode" as you mentioned last time ? I know I probably asked too many questions. Your any reply would be greatly appreciated.. :)

                    C 1 Reply Last reply
                    0
                    • C Christian Graus

                      You cna pass the buffer into a CBitmap constructor, although I'm not sure if a printer DC will print any bit depth DDB. I'd play it safe and create a DIBSection, then you can just memset the buffer into it. If you call CreateDIBSection, you get a pointer to the data and you can copy your buffer into it. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

                      Sonork ID 100.10002:MeanManOz

                      I live in Bob's HungOut now

                      M Offline
                      M Offline
                      manio
                      wrote on last edited by
                      #12

                      If I use StretchBlt, can I print "color" image ? (I mean, if my printer is color...) Another API I found is: DrawDibDraw, can I also use this API to print "color" image ?

                      C 1 Reply Last reply
                      0
                      • M manio

                        Christian, One more question about StretchBlt.. suppose my bitmap data to be printed is in some buffer like this: pCurrentImage, (i.e, as the following class CRGBImage { public: BYTE* data; int m_nWidth; // in pixel int m_nHeight; }; Now I already get something like this: CRGBImage* pCurrentImage... Then I want to print this image data, so I wrote the following codes. CPrintDialog dlg( FALSE ); CDC dc; dc.Attach( dlg.GetPrinterDC() ); //calculate printing area(width and height) int pwidth = dc.GetDeviceCaps(HORZRES);//get width of printing area int pheight = dc.GetDeviceCaps(VERTRES);//get height of printing area Now, how can I copy my bitmap data to dc ? And, for the following StretchBlt API: BOOL CDC::StretchBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, DWORD dwRop ); (Take the width for example) The above parameter nSrcWidth = pCurrentImage->m_nWidth, right ? And, how about the nWidth ? How to calculate it if I want to print to the original dimension of my bitmap ? How to calculate pSrcDC ? (Do I just use: dc.StretchBlt(...) as dc mentioned above ? Because there are 2 DCs, I got kind of confused !! ) And, when to put "COLORONCOLOR StretchBlt mode" as you mentioned last time ? I know I probably asked too many questions. Your any reply would be greatly appreciated.. :)

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #13

                        manio wrote: Now, how can I copy my bitmap data to dc ? Not AFAIK, in that the command that does this is StretchBlt. I would imagine writing an equally efficient streching routine that does COLORONCOLOR would be tedious, and kind of redundant when it's in front of you. Just create a DIBSection with the width and height, you have a DSC if you want, but if you don't specify PAL_COLORS the DC paramater can be NULL anyhow. Then bitblt your bits into it, and stretchblt to the screen. Yes, you need to make another copy to do that, but that's really the best way I can see. manio wrote: And, how about the nWidth ? How to calculate it if I want to print to the original dimension of my bitmap ? Like I said before, your original bitmap has a dimension that relates to the resolution and size of your monitor - it's NOT a real number, it's a number of pixels. Or do you mean aspect ratio ? Use the ratio of bitmap width to height to calculate the size of the aspect you're not filling the page with. If that comes out too big for the page, use the other one. For example, if my bitmap is 2 x 3 pixels and I am drawing with a width of 6, I get the height by going 6 * 3 /2 = 9. manio wrote: How to calculate pSrcDC ? (Do I just use: dc.StretchBlt(...) as dc mentioned above ? Because there are 2 DCs, I got kind of confused !! ) Once you've created the DIBSection you need to select the HBITMAP returned from CreateDIBSection into a DC. Create it like this: CDC srcDC; srcDC.CreateCompatibleDC(&dc); Now the DC is compatible with the print DC. Select your image into it, and stretchblt. Don't forget to go dc.SetStretchBltMode(COLORONCOLOR); manio wrote: I know I probably asked too many questions. Your any reply would be greatly appreciated.. Printing is a pain, printing graphics doubly so. I hope this helped. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

                        Sonork ID 100.10002:MeanManOz

                        I live in Bob's HungOut now

                        1 Reply Last reply
                        0
                        • M manio

                          If I use StretchBlt, can I print "color" image ? (I mean, if my printer is color...) Another API I found is: DrawDibDraw, can I also use this API to print "color" image ?

                          C Offline
                          C Offline
                          Christian Graus
                          wrote on last edited by
                          #14

                          manio wrote: f I use StretchBlt, can I print "color" image ? (I mean, if my printer is color...) OF course, that is what COLORONCOLOR is for, to smooth the colours when stretching. manio wrote: Another API I found is: DrawDibDraw, can I also use this API to print "color" image ? Must be - no point having a 2 color DIB. I believe this API is part of VFW, I know Chris' DIBSection wrapper uses it and I believe it does dithering. But I've never used it so I can't comment beyond that. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

                          Sonork ID 100.10002:MeanManOz

                          I live in Bob's HungOut now

                          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