//
HDC hPrinterDC = NULL; /*printer dc*/
HDC hWinDC = NULL; /*windows dc*/
DOCINFO di = {0};
float fLogPelsX1, fLogPelsY1; /*windows log pixelsx*/
float fLogPelsX2, fLogPelsY2; /*print driver log pixelsx*/
float fScaleX, fScaleY;
//
memset( &di, 0, sizeof(DOCINFO) );
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Bitmap Printing Test";
di.lpszOutput = (LPTSTR) NULL;
//di.lpszDataType = (LPTSTR) NULL;
di.fwType = 0;
hPrinterDC = CreateDC(NULL, MY_VPRINTER, NULL, NULL);
// Begin a print job by calling the StartDoc function.
int nError = StartDoc(hPrinterDC, &di);
// if (nError == SP_ERROR)
// {
// //errhandler("StartDoc", hwnd);
// goto Error;
// }
// Inform the driver that the application is about to begin
// sending data.
// nError = StartPage(hPrinterDC);
// if (nError <= 0)
// {
// //errhandler("StartPage", hwnd);
// goto Error;
// }
// Retrieve the number of pixels-per-logical-inch in the
// horizontal and vertical directions for the display upon which
// the bitmap was created. These are likely the same as for
// the present display, so we use those values here.
//hWinDC = GetDC(hWnd);
hWinDC = CreateDC (TEXT("DISPLAY"), NULL, NULL, NULL);
fLogPelsX1 = (float) GetDeviceCaps(hWinDC, LOGPIXELSX);
fLogPelsY1 = (float) GetDeviceCaps(hWinDC, LOGPIXELSY);
// Retrieve the number of pixels-per-logical-inch in the
// horizontal and vertical directions for the printer upon which
// the bitmap will be printed.
fLogPelsX2 = (float) GetDeviceCaps(hPrinterDC, LOGPIXELSX);
fLogPelsY2 = (float) GetDeviceCaps(hPrinterDC, LOGPIXELSY);
// Determine the scaling factors required to print the bitmap and
// retain its original proportions.
if (fLogPelsX1 > fLogPelsX2)
fScaleX = (fLogPelsX1 / fLogPelsX2);
else fScaleX = (fLogPelsX2 / fLogPelsX1);
if (fLogPelsY1 > fLogPelsY2)
fScaleY = (fLogPelsY1 / fLogPelsY2);
else fScaleY = (fLogPelsY2 / fLogPelsY1);
// // Compute the coordinates of the upper left corner of the
// // centered bitmap.
//
// int cWidthPels = GetDeviceCaps(hPrinterDC, HORZRES);
// int xLeft = ((cWidthPels / 2) - ((int) (((float) bmih.biWidth)
// * fScaleX)) / 2);
// int cHeightPels = GetDeviceCaps(hPrinterDC, VERTRES);
// int yTop = ((cHeightPels / 2) - ((int) (((float) bmih.biHeight)
// * fScaleY)) / 2);
//
//UINT frameCount = GetImgFrameCount("C://Microsoft Word - sjzl20086412-ZXUP10 统一业务开放平台 SoftDA业务 客户端_V1.03.10.tif");
USES_CONVERSION;
LPCTSTR lpFilePath = "D://Microsoft Word - sjzl20086412-ZXUP10 统一业务开放平台 SoftDA业务 客户端_V1.03.10.tif";
WCHAR *lpwcFileName = NULL;
lpwcFileName = A2W(lpFilePath);
//
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Image* image = new Image(lpwcFileName);//(L"C://Microsoft Word - ecgprint阅读笔记.tif");//(L"Multiframe.tif");
// How many frame dimensions does the Image object have?
UINT count = 0;
count = image->GetFrameDimensionsCount();
//printf("The number of dimensions is %d./n", count);
GUID* pDimensionIDs = (GUID*)malloc(sizeof(GUID)*count);
// Get the list of frame dimensions from the Image object.
image->GetFrameDimensionsList(pDimensionIDs, count);
// Display the GUID of the first (and only) frame dimension.
WCHAR strGuid[39];
StringFromGUID2(pDimensionIDs[0], strGuid, 39);
//wprintf(L"The first (and only) dimension ID is %s./n", strGuid);
// Get the number of frames in the first dimension.
UINT frameCount = image->GetFrameCount(&pDimensionIDs[0]);
//printf("The number of frames in that dimension is %d./n", frameCount);
//
// Compute the coordinates of the upper left corner of the
// centered bitmap.
int cWidthPels = GetDeviceCaps(hPrinterDC, HORZRES);
int xLeft = ((cWidthPels / 2) - ((int) (((float) image->GetWidth())
* fScaleX)) / 2);
int cHeightPels = GetDeviceCaps(hPrinterDC, VERTRES);
int yTop = ((cHeightPels / 2) - ((int) (((float) image->GetHeight())
* fScaleY)) / 2);
//
UINT imgWidth = image->GetWidth();
UINT imgHeight = image->GetHeight();
UINT fIndex = 0;
while(fIndex < frameCount)
{
image->SelectActiveFrame(&pDimensionIDs[0],fIndex);
nError = StartPage(hPrinterDC);
Graphics graphics(hPrinterDC); //hDC是外部传入的画图DC
//graphics.DrawImage(image,0,0,image->GetWidth(),image->GetHeight());
RectF destRect(xLeft, yTop, (int) ((float) imgWidth * fScaleX), (int) ((float) imgHeight * fScaleY));
//RectF srcRect(0, 0, imgWidth, imgHeight);
graphics.DrawImage(image,destRect,0, 0, imgWidth, imgHeight, UnitPixel);
//graphics.DrawImage(image,destRect,srcRect,UnitInch,NULL);
//重新设置当前的活动数据帧
//image->SelectActiveFrame(&pDimensionIDs[0],fIndex++);
fIndex++;
nError = EndPage(hPrinterDC);
}
//
free(pDimensionIDs);
delete(image);
GdiplusShutdown(gdiplusToken);
// Use StretchDIBits to scale the bitmap and maintain
// its original proportions (that is, if the bitmap was square
// when it appeared in the application's client area, it should
// also appear square on the page).
// if (StretchDIBits(hPrinterDC, xLeft, yTop, (int) ((float) bmih.biWidth
// * fScaleX), (int) ((float) bmih.biHeight * fScaleY), 0, 0,
// bmih.biWidth, bmih.biHeight, lpBits, lpBitsInfo, iUsage,
// SRCCOPY) == GDI_ERROR)
// {
// //errhandler("StretchDIBits Failed", hwnd);
// }
// Retrieve the width of the string that specifies the full path
// and filename for the file that contains the bitmap.
//GetTextExtentPoint32(pd.hDC, ofn.lpstrFile,
//ofn.nFileExtension + 3, &szMetric);
// Compute the starting point for the text-output operation. The
// string will be centered horizontally and positioned three lines
// down from the top of the page.
//xLeft = ((cWidthPels / 2) - (szMetric.cx / 2));
//yTop = (szMetric.cy * 3);
// Print the path and filename for the bitmap, centered at the top
// of the page.
//TextOut(pd.hDC, xLeft, yTop, ofn.lpstrFile,
//ofn.nFileExtension + 3);
// Determine whether the user has pressed the Cancel button in the
// AbortPrintJob dialog box; if the button has been pressed, call
// the AbortDoc function. Otherwise, inform the spooler that the
// page is complete.
// nError = EndPage(hPrinterDC);
// if (nError <= 0)
// {
// //errhandler("EndPage", hwnd);
// goto Error;
// }
// Inform the driver that document has ended.
nError = EndDoc(hPrinterDC);
// if (nError <= 0)
// ;//errhandler("EndDoc", hwnd);
//Error:
// Enable the application's window.
//EnableWindow(hwnd, TRUE);
// Remove the AbortPrintJob dialog box.
//DestroyWindow(hdlgCancel);
// Delete the printer DC.
//DeleteDC(hPrinterDC);