The following is the code I used to control printing pictures:
using namespace Gdiplus;
void printImage(std::wstring printerDriverName, std::wstring printerName, std::wstring printerPortName, PDEVMODEW pDevMode)
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
HDC hDC = ::CreateDCW(printerDriverName.c_str(),printerName.c_str(), printerPortName.c_str(), pDevMode);
DOCINFOW sDocInfo;
memset(&sDocInfo, 0, sizeof(DOCINFOW));
sDocInfo.cbSize = sizeof(sDocInfo);
sDocInfo.lpszDocName = "test";
::StartDocW(hdc, &sDocInfo);
// Printed area
int printLeft = ::GetDeviceCaps(hDC, PHYSICALOFFSETX);
int printLop = ::GetDeviceCaps(hDC, PHYSICALOFFSETY);
int printWidth = ::GetDeviceCaps(hDC, HORZRES);
int printHeight = ::GetDeviceCaps(hDC, VERTRES);
// Load PNG pictures
std::unique_ptr<Bitmap> pBitmap = std::make_unique<Bitmap>("C:/Desktop/test.png");
if (Ok == pBitmap->GetLastStatus())
{
int imageWidth = pBitmap->GetWidth();
int imageHeight = pBitmap->GetHeight();
// Calculate the zoom ratio of pictures on paper
float scaleX = static_cast<float>(printWidth) / imageWidth;
float scaleY = static_cast<float>(printHeight) / imageHeight;
float scale = std::min(scaleX, scaleY);
// Calculating the zoom pictures on the paper position, print in the middle
int printImageWidth = qRound(imageWidth * scale);
int printImageHeight = qRound(imageHeight * scale);
int printImageX = (printWidth - printImageWidth) / 2;
int printImageY = (printHeight - printImageHeight) / 2;
// Start printing
::StartPage(hdc);
Graphics printGraphics(hDC);
printGraphics.SetPageUnit(UnitPixel);
printGraphics.DrawImage(pBitmap.get(), printImageX, printImageY, printImageWidth, printImageHeight);
::EndPage(hdc);
}
::EndDoc(hdc);
::DeleteDC(hDC);
GdiplusShutdown(m_gdiplusToken);
}
Printing pictures with this code, the pictures printed on my printer are normal, but the pictures printed on some printers are missing a part. Has anyone had a similar problem? Please help me
My guess is that the print area obtained is not right