Reputation: 2500
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());
PrinterDC
ImageDC
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?
Thanks to VTT's answer I was able to fix this problem with the following code
CDC memDC;
memDC.CreateCompatibleDC(&PrinterDC);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&PrinterDC, PrinterDC.GetDeviceCaps(HORZRES), PrinterDC.GetDeviceCaps(VERTRES));
CBitmap *pOldBitmap = memDC.SelectObject(&bitmap);
memDC.Rectangle(0, 0, PrinterDC.GetDeviceCaps(HORZRES), PrinterDC.GetDeviceCaps(VERTRES));
// draw into memDC here
CImage Image;
Image.Attach(bitmap);
Image.Save(L"path/to/file.png", Gdiplus::ImageFormatPNG);
memDC.SelectObject(pOldBitmap);
Upvotes: 1
Views: 866
Reputation: 37468
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.
Upvotes: 1