About the printing of bitmaps, many people are confused, there are many problems in this regard, including the following points.
1: The picture of printing is too small;
2: Fundamental printing does not come out
3: Print preview visible, but printing can not come out;
The root cause of these problems is that the understanding of the bitmap is not deep enough.
A cbitmap object, may be a DDB bitmap (device-related bitmap), or a DIB bitmap (device-free bitmap), where a device is compatible with memory DC, you can only select the device compatible DDB bitmap or monochrome Bitmap. Note that the bitmap can only be selected into the device-compatible memory DC, and cannot choose to enter the true DC, which is sometimes the reason why it does not print at all.
As for the problem of printing pictures, it is mainly because the unit of the bitmap size is the number of horizontal and longitudinal pixels, rather than exact length, so if a picture is mapped to the screen, there will be a relatively large image, but The resolution of the printer is much higher than the screen (the screen is typically 96dpi, and the printer is generally generally 300dpi). If the bitmap is not zoomed to the printer, it must be a lot. The method of solving this problem is to use StretChblt to stretch the display image.
In addition, the DIB bitmap generated by the LR_CREATEDIBSECTION option in the loadImage function can be selected into any device-compatible memory DC. So you can load a bitmap file or resource using the loadImage function, and print the bitmap directly through Stretchblt. Here is the related code for printing a bitmap file:
/ *
// Print or draw bitmap files on the upper left corner of the screen
/ / PDC printer or screen DC pointer
Ilogpixelx
Ilogpixely
The GetDeviceCaps (Logpixelsx) value of the screen DC, where
Ilogpixelx = dc.getDeviceCaps (LogPixelsx);
Ilogpixely = dc.getDeviceCaps (Logpixelsy);
Const char * strfilename BMP image file name
* /
Void DrawBMP (CDC * PDC, INT ILOGPIXELX, ILOGPIELY, Const Char * Strfilename)
{
CDC MEMDC; // Memory device environment pointer, the entire existence process will exist
CBITMAP Bitmap, * PoldBMP;
CRECT SOURCE, DEST; // Record source bitmap size and final display size
Bitmap BM;
IF (MEMDC.GETSAFEHDC () == NULL)
{
Hbitmap hbitmap = (hbitmap) loading (0, strfilename, image_bitmap, 0, 0, lr_createdibsection | lr_defaultsize | lr_loadfromfile);
Bitmap.attach (hbitmap);
MEMDC.CREATECOMPALEDC (PDC);
Bitmap.GetObject (Sizeof (BM), & BM);
PoldBMP = MEMDC.SELECTOBJECT (& BITMAP);
Source.top = 0;
Source.Left = 0;
Source.right = bm.bmwidth;
Source.bottom = BM.BMHEIGHT;
Dest = Source;
}
PDC-> DPTOLP (& DEST);
IF (PDC-> isprinting ()) {
DEST.LEFT = (int) (DOUBLE) PDC-> getDeviceCaps (Logpixelsx)) / iLogpixelx);
DEST.right = (int) (DEST.Right * ((Double) PDC-> getDeviceCaps (Logpixelsx)) / iLogpixelx);
DEST.TOP = (int) (DEST.TOP * ((Double) PDC-> getDeviceCaps (Logpixelsy)) / iLogpixally
DEST.BOTTOM = (int) (DOUBLE) PDC-> getDeviceCaps (Logpixelsy)) / iLogpixally
}
PDC-> Stretchblt (dest.Lest, Dest.top, DEST.RIGHT, DEST.BOTTOM,
& MEMDC, Source.Left, Source.top, Source.right, Source.Bottom, Srcopy;
MEMDC.SELECTOBJECT (POLDBMP);
Bitmap.deleteObject ();
MEMDC.DELETEDC ();
Return;
}
But for the printing of the display device-compatible DDB bitmap, it is not that simple, such as screenshots, this bitmap cannot choose to enter the printing device-compatible memory DC, for this problem, generally converted to DIB bitmap , Then use the StretchDibits function to display the bitmap on the DC, below is a function code that can print any bitmap.