How to make scaled SVG for QImage?

The QtSvg module has a QSvgRenderer class that can display an image on a QPaintDevice . It could be a QImage . In this case, we will create:

 Image svgBufferImage(renderer.defaultSize(), QImage::Format_ARGB32); 

But how to render in QImage different sizes than the default from SVG rendering? Since an SVG image can be scaled without loss of quality, is it possible to create static images, such as PNG, from SVG files using QSvgRenderer ?

Anyone have a better idea? Basically I need to create images like PNG from SVG files of different sizes.

+13
qt image svg
Dec 18 '11 at 12:28
source share
2 answers

Just specify the QImage size you QImage . The SVG renderer will scale to fit the entire image.

 #include <QApplication> #include <QSvgRenderer> #include <QPainter> #include <QImage> // In your .pro file: // QT += svg int main(int argc, char **argv) { // A QApplication instance is necessary if fonts are used in the SVG QApplication app(argc, argv); // Load your SVG QSvgRenderer renderer(QString("./svg-logo-h.svg")); // Prepare a QImage with desired characteritisc QImage image(500, 200, QImage::Format_ARGB32); image.fill(0xaaA08080); // partly transparent red-ish background // Get QPainter that paints to the image QPainter painter(&image); renderer.render(&painter); // Save, image format based on file extension image.save("./svg-logo-h.png"); } 

This will create a PNG image of size 500x200 from the transferred in the SVG file.

Example output with an SVG image from an SVG page :

enter image description here

+31
Dec 18 '11 at 12:49
source share

Here is the complete answer:

 QImage QPixmap::toImage() 

If pixmap has a 1-bit depth, the returned image will also have a depth of 1 bit. Images with a large number of bits will be returned in a format that represents the underlying system. Usually it will be QImage :: Format_ARGB32_Premultiplied for pixmaps with alpha and QImage :: Format_RGB32 or QImage :: Format_RGB16 for pixmaps without alpha.

 QImage img = QIcon("filepath.svg").pixmap(QSize(requiredsize)).toImage() 

Also copy the answer above

 // Save, image format based on file extension image.save("./svg-logo-h.png"); 
+2
May 13 '16 at 8:30
source share



All Articles