PDA

View Full Version : SVG to QImage and keep ratio



lni
21st November 2013, 14:26
Hi,

I need to create an QImage from svg file. How do I keep the ration? Also how do I get all element ids in the svg file?

Here is the code to start. Many thanks.



#include <QApplication>
#include <QSvgRenderer>
#include <QPainter>
#include <QImage>
#include <QDebug>

// 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("./TW_CHW194.svg"));

qDebug() << "size =" << renderer.defaultSize();

// Prepare a QImage with desired characteritisc
QImage image(100, 500, 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-2-image.png");
}