-1

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

genpfault
  • 51,148
  • 11
  • 85
  • 139
douxiaobao
  • 11
  • 2
  • " but the pictures printed on some printers are missing a part" Could you please tell us what circumstances the pictures printed are missing a part? Which version of Windows are you using? You could refer to the Doc:[Sending GDI+ Output to a Printer](https://learn.microsoft.com/en-us/windows/win32/gdiplus/-gdiplus-sending-gdi-output-to-a-printer-use) and the thread:https://stackoverflow.com/questions/66872343/gdi-memory-leak-when-printing-png24-images-with-gdiplus – Jeaninez - MSFT Aug 24 '23 at 02:36
  • The image is missing in part because the normal thing is that after the DrawImage() method, the size of the image is extended to the same size as the paper size, and the full print is printed on paper, but on a small number of printers after the DrawImage() method, the size of the image is expanded to be larger than the paper size, resulting in only a part of the image being printed on paper. Why is this the case? – douxiaobao Aug 24 '23 at 03:53
  • As far as I'm concerned, you need to specifies the location and size of the drawn image. And then the image is scaled to fit the rectangle. The parameters that you are passing into the DrawImage method should be the size you want the image on the paper rather than the size of the image itself, the DrawImage command will then take care of the scaling for you. I suggest you could refer to the thread: https://stackoverflow.com/questions/9982579/printing-image-with-printdocument-how-to-adjust-the-image-to-fit-paper-size – Jeaninez - MSFT Aug 24 '23 at 06:39
  • Thank you very much for your reply, but the parameter I passed to DrawImage is the size of the paper. Normally, the image should be as large as the paper, but the anomaly is that the image is stretched larger than the size of the paper. Is it because the DPI of the printer is different from the DPI of the picture? – douxiaobao Aug 24 '23 at 12:34
  • Refer to the Doc:[Cropping and scaling GDI+ images](https://learn.microsoft.com/en-us/windows/win32/gdiplus/-gdiplus-cropping-and-scaling-images-use) The DrawImage method determines where to draw the cropped apple and how big to make the cropped apple by looking at the destination rectangle, which is specified by the second argument. In my opinion, this episodic phenomenon may be related to the printer's settings. You could try to set [DEVMODEA structure](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-devmodea?redirectedfrom=MSDN). – Jeaninez - MSFT Aug 25 '23 at 07:31

0 Answers0