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.Use the BitBlt function to copy the bitmap from the memory device context to the printer.
5.End the print request.
6.Remove the bitmap from the memory device context and delete the device context.
The following example shows how to print a bitmap named dog that has been added to the resource file:
HDC hDC;
HDC hMemoryDC;
HDC hPr;
BITMAP Bitmap;
.
.
.
1 hDC = GetDC(hWnd);
hMemoryDC = CreateCompatibleDC(hDC);
ReleaseDC(hWnd, hDC);
2 hBitmap = LoadBitmap(hInstance, “dog”);
3 GetObject(hBitmap, sizeof(BITMAP), (LPSTR) &Bitmap);
4 hOldBitmap = SelectObject(hMemoryDC, hBitmap);
5 hPr = CreateDC(“EPSON”,
“EPSON FX-80”,
“LPT1:”,
(LPSTR) NULL);
if(hPr != NULL) {
Escape (hPr, STARTDOC, 4, (LPSTR) “Dog”, 0L);
6 BitBlt(hPr, 10, 30,
Bitmap.bmWidth,
Bitmap.bmHeight,
hMemDC, 0, 0, SRCCOPY);
7 Escape(hPr, NEWFRAME, 0, 0L, 0L);
Escape(hPr, ENDDOC, 0, 0L, 0L);
DeleteDC(hPr);
}
8 SelectObject(hMemoryDC, hOldBitmap);
DeleteDC(hMemoryDC);
DeleteObject(hBitmap);
In this example:
1 | The application retrieves the current window's display context using the GetDC function. The CreateCompatibleDC function then creates a memory device context that is compatible with that display context. After creating the memory device context, the application releases the window's display context using the ReleaseDC function. |
2 | The LoadBitmap function loads the bitmap dog from the application's resources. |
3 | The GetObject function retrieves information about the bitmap, such as its height and width. These values are used later in the BitBlt function. |
4 | The SelectObject function selects the bitmap into the memory device context. |
5 | 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. |
6 | 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.) |
7 | The statements that send the NEWFRAME and ENDDOC escape sequences are identical to those used in the previous example. |
8 | 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. |