Resize A Picture When Print it

  • Hello
    i have a app that take a screenshot of window and i want to Print that Image
    but i want to when i want to print program fit the image with paper size like A4 Size
    how i can resize the image when Printing?
    and i have second Question
    What is The Base Of sizes in Qt?
    Thanks a lot

  • I think the best opportunity would be to generate a pdf and then print that.

    QPrinter printer(QPrinter::HighResolution);
    printer.setPageMargins(0,0,0,0, QPrinter::Millimeter);
    QPainter painter(&printer);

    For the base of size take a look at

  • thanks so much
    but i want only print a .png file in a directory not a pdf!
    can you Guidance ?

  • Lifetime Qt Champion

    He do not print a PDF in that code.
    He print Image to a pdf file.
    You should read the docs.
    Many good examples.

  • @mrjj
    yes you say right :)
    and if i want print without print Dialog how should do that?

  • Lifetime Qt Champion

    He dont use print dialog.

  • @mrjj
    i do this but my image in PDF file is not full screen!
    its very small in PDF File!

  • Lifetime Qt Champion

    yes, that is normal.
    Printer have many more pixels. so image seem smaller.
    Read docs.
    they have example of scaling stuff.
    This sample takes snapshot and scales it.

    void MainWindow::printshot() {
      QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId());
      QPrinter printer(QPrinter::HighResolution);
      QPainter painter;
      double xscale = printer.pageRect().width() / double(pix.width());
      double yscale = printer.pageRect().height() / double(pix.height());
      double scale = qMin(xscale, yscale);
      painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                        printer.paperRect().y() + printer.pageRect().height() / 2);
      painter.scale(scale, scale);
      painter.translate(-width() / 2, -height() / 2); // note uses the form width/height! use pix.h/w if random image
      painter.drawPixmap(0, 0, pix);