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
// Load your SVG
qDebug() << "size =" << renderer.defaultSize();
// Prepare a QImage with desired characteritisc
image.fill(0xaaA08080); // partly transparent red-ish background
// Get QPainter that paints to the image
renderer.render(&painter);
// Save, image format based on file extension
image.save("./svg-2-image.png");
}
#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");
}
To copy to clipboard, switch view to plain text mode
Bookmarks