How to create an image of a specific width and height
-
Halo,
I must create a QImage of specified size in inches. For example 6 inches wide and 4.25 inches high. The paper size and margins can change, so I must allow for that.
I need to avoid scaling.
I am using QT5.1 rc on Ubuntu linux.
I tried many variations of the following code, but it does not work as expected, and by trial-and-error found that if I hardcode DPI to 96 its gets close, but do not understand why or how
this would need to change with a change in paper size, orientation or margins.@
#include <QApplication>
#include <QTextDocument>
#include <QTextCursor>
#include <QPrinter>
#include <QAbstractTextDocumentLayout>int main(int argc, char *argv[])
{
QApplication app(argc, argv);// set up the printer QPrinter printer(QPrinter::HighResolution); printer.setOutputFormat(QPrinter::PdfFormat); printer.setOrientation(QPrinter::Portrait); printer.setPaperSize(QPrinter::Letter); printer.setPageMargins(6.35,12.7,6.35,12.7,QPrinter::Millimeter); printer.setResolution(1200); printer.setFullPage(true); printer.setColorMode(QPrinter::Color); // create any empty document QTextDocument doc; // set up document layout, page size, etc doc.documentLayout()->setPaintDevice(&printer); doc.setPageSize(QSizeF(printer.pageRect().size())); // create the image of specified size const float widthInches=6.0; const float heightInches=4.25; // QImage w and h must be in pixels that match the paint device? const int xDPI=doc.documentLayout()->paintDevice()->physicalDpiX(); const int yDPI=doc.documentLayout()->paintDevice()->physicalDpiY(); const float w=widthInches*xDPI; const float h=heightInches*yDPI; QImage theImage(w,h,QImage::Format_RGB32); // just fill it with color for example theImage.fill(Qt::blue); // put the image in the document QTextCursor cursor(&doc); cursor.insertImage(theImage); // create the PDF printer.setOutputFileName(QString("myDoc.pdf")); doc.print(&printer); return 0; }
@
Any help is appreciated greatly.
Kind Regards,
Vic -
Hi Victor.
In line 17 of your code, you are setting printer resolution to 1200 dpi.
But in lines 39, 40 you are using 96 as dpi.
In base of that, you must to change line 17 and set printer resolution to 96 or change lines 39, 40 and replace to 1200 dpi.
Regards. -
Sergio,
Thank you for your reply.
Line 32 defines the macro so that lines 35 and 36 are compiled. This is what I need to make work (because the paper size or image size or resolution could change too, so I need a programmatic way to pick up the values).
Using lines 35 and 36 in fact results in the value 1200 for xDPI and yDPI, but as mentioned originally this produces a horribly wrong result in the pdf.
Any ideas?
-
Hi Victor, Sorry for delay.
I'm working in a little project and to make a barcode, I use "QPixmap":http://qt-project.org/doc/qt-5.0/qtgui/qpixmap.html , something like
@QPixmap pixmap (w , h);
...
painter.setFont(fontA);
painter.drawText(QRect(0,45mm,85.6mm,5mm), Qt::AlignHCenter, cliente);
painter.drawPixmap(QRect(5mm, 0mm, 75.5mm, 45mm), pm.scaledToWidth(75.5mm));@
See line 5 specially.
I hope it's util for you.
Regards -
What is the value for 'w' and 'h' and what is the relationship to to
the values in QRect.. 5, 75.5 and 45? Waht is the value of mm?My apologies for being obtuse, but it is my lack of understanding in general concerning the relationships that is my problem, I believe.
To take your example and turn it into a compilable function that looks like this?
@
void printBarcode(QString barcodeValue,int widthMM,int heightMM) // where widthMM and heightMM is the true printed size in the barcode image
{
// calculate w and h
const int w= // what is the math involving widthMM ?
const int h = // what is the math involving heightMM?
QPixmap pixmap(w,h);
const int mm = // how to calculate this conversion factor? what is the value?
const int a= // what is the math? how is '5' calculated?
const int b= //
const int c = //
const int d = // what is the relationship to widthMM and heightMM?
painter.drawPixmap(QRect(amm,bmm,cmm,dmm,pixmap.scaledToWidth(c*mm));
// why are we not scaling to both heightMM and widthMM?
}
@ -
w and y are values you wish as width and heigh of your images.
mm represent pixels per milimeters.Here basically, create a pixmap image with "x" by "y" pixels.
Then scale this pixmap to size wished.Regards.
-
Sorry for not mentioning this earlier, but I thought my request was clear enough: I do not want to rescale because this causes quality issues with fonts, etc.
I know up front that I want the image to be a certain width and height in millimeters on the printed page.