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?
    Inch?Pixel?Centimeter?
    Thanks a lot



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

    QPrinter printer(QPrinter::HighResolution);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setResolution(300);
    printer.setOutputFileName("patth/to/your/pdffile.pdf");
    printer.setPageMargins(0,0,0,0, QPrinter::Millimeter);
    
    printer.setPaperSize(QPrinter::A4);
    
    QPainter painter(&printer);
    painter.drawImage(0,0,QImage());
    painter.end();
    

    For the base of size take a look at http://doc.qt.io/qt-5/qprinter.html#Unit-enum.



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


  • Qt Champions 2016

    Hi
    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 :)
    Thanks
    and if i want print without print Dialog how should do that?


  • Qt Champions 2016

    Hi
    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!


  • Qt Champions 2016

    @M4RZB4Ni
    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);
      printer.setOrientation(QPrinter::Landscape);
    
      QPainter painter;
      painter.begin(&printer);
      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);
      painter.end();
    }
    


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.