Printing a bitmap is similar to printing a line of text. To print a bitmap, follow these steps:
1.Create a memory device context that is compatible with the bitmap.
2.Load the bitmap, and select it into the memory device context.
3.Start the print request.
4.Indicate the beginning of a new page frame.
5.Use the BitBlt function to copy the bitmap from the memory device context to the printer.
6.Indicate the ending of the current page frame.
7.End the print request.
8.Remove the bitmap from the memory device context and delete the device context.
The following example prints a bitmap named Dog that has been added to the resource-definition file.
char pPrintInfo[80];
LPSTR lpTemp;
LPSTR lpPrintDevice;
LPSTR lpPrintDriver;
LPSTR lpPrintPort;
HDC hdcPrint, hdcMemory;
HBITMAP hBitmap, hOldBitmap;
BITMAP Bitmap;
DOCINFO DocInfo; /* used in StartDoc function */
/*
* Use GetProfileString to retrieve strings declared above and
* parse them as shown in the earlier example.
*/
hdcPrint = CreateDC(lpPrintDriver,
lpPrintDevice,
lpPrintPort,
(LPSTR) NULL);
if (hdcPrint != NULL) {
hdcMemory = CreateCompatibleDC(hdcPrint);
hBitmap = LoadBitmap(hAppInstance, "Dog");
GetObject(hBitmap, sizeof(BITMAP), &Bitmap);
hOldBitmap = SelectObject(hdcMemory, hBitmap);
DocInfo.cbSize = sizeof(DOCINFO);
DocInfo.lpszDocName = "Test";
DocInfo.lpszOutput = (LPSTR) NULL;
StartDoc(hdcPrint, &DocInfo);
StartPage(hdcPrint);
BitBlt(hdcPrint, 10, 30,
Bitmap.bmWidth,
Bitmap.bmHeight,
hdcMemory, 0, 0, SRCCOPY);
EndPage(hdcPrint);
EndDoc(hdcPrint);
DeleteDC(hdcPrint);
SelectObject(hdcMemory, hOldBitmap);
DeleteDC(hdcMemory);
DeleteObject(hBitmap);
}
In this example, the application retrieves the printer device context. The CreateCompatibleDC function then creates a memory device context that is compatible with the printer's device context.
The LoadBitmap function loads the bitmap Dog from the application's resources, and the GetObject function retrieves information about the bitmap, such as its height and width. These values are used later in the BitBlt function.
The SelectObject function selects the bitmap into the memory device context.
The statements for creating the printer device context and starting the print request are identical to those used in the example that printed a line of text.
To send the bitmap image to the printer, the application uses the BitBlt function. BitBlt copies the bitmap from the memory device context to the printer, placing the bitmap at the coordinates (10,30). (The BitBlt function takes the place of the TextOut function, used in the previous example to print a line of text.)
The statements that end the current page frame and end the document are identical to those used in the previous example.
After the print request is complete, the SelectObject and DeleteDC functions remove the bitmap from selection and delete the memory device context. Since the bitmap is no longer needed, the DeleteObject function removes it from memory.