sigy sigy - 11 months ago 85
C++ Question

Save printer device context as image

My code prints some reports by giving the data, a printer's device context and a bounding rectangle to a third party library. This library then renders the data into the given device context which I can send to the printer afterwards. I now need to save this exact document so that the user can print it again at a later time. The easiest solution I could think of is to give the device context of an image file to the third party library, let it draw into the image and save the image as a file. However the results are not identical. For example the lines in the image are thicker than the ones send to the printer, increased line spacing between two lines, etc.

The DC I get from the printer obviously has different properties (e.g. higher DPI and thus different dimensions when measuring in pixels). I tried to adjust for that and calculated the dimensions of the image in accordance with the DPI.

What I don't understand is why the image's dimensions are different from the corresponding DC of the image. I create it with

CImage Image;
Image.Create(widthAt96Ppi, heightAt96Ppi, 32);
CDC* pDC = CDC::FromHandle(Image.GetDC());


  • DPI: 600 / 600

  • Dimensions of the printer's dc in device Units:
    4958 / 7017

  • Dimensions of the printer's dc in pixels: 4958 / 7016

  • Bounding rect: 4718 / 6776

  • GetDeviceCaps(ASPECTX)/GetDeviceCaps(ASPECTY): 600 / 600


  • DPI: 96 / 96

  • Dimensions of the image's dc in device Units: 0 / 0

  • Dimensions of the image's dc in pixels: 1920 / 1200

  • Dimensions of the image in pixels: 793 / 1122

  • Bounding rect: 755 / 1084

  • GetDeviceCaps(ASPECTX)/GetDeviceCaps(ASPECTY): 36 / 36

Why do I get different results? The third party library only gets a CDC pointer so the CDC must contain information that causes these differences. But how can I adjust them so that the results look the same?

Answer Source

It looks like your bitmap is being created compatible to desktop DC. I think you should create image using CreateCompatibleBitmap method (or corresponding MFC overload, if it has one) supplying original printer DC and dimensions to it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download