Page 1 of 1

[C++ API] Convert svg to png with transparent background

Posted: 2013-05-24T09:04:35-07:00
by henrique
Hi.

I'm new to ImageMagick, and I've been trying to use the C++ API to convert an SVG file to a PNG. The problem is that the resulting PNG file's background always defaults to white. I've been searching for solutions for this, but all I find is the solution using the comand line

Code: Select all

convert -background none svg_image.svg png_image.png
This command line does exactly what I want, but I can't seem to find a way to do it with the C++ API. Here is the code that I'm using to convert the image to PNG:

Code: Select all

Magick::Image svgImage(svgFilePath);
svgImage.magick("png");
Magick::Blob blob;
svgImage.write(&blob);
Is this possible to do and if so, how can I do it?

Thanks!

Re: [C++ API] Convert svg to png with transparent background

Posted: 2013-05-24T18:24:39-07:00
by magick
Declare an image and set the background color, e.g. example.backgroundColor( "#000000FF" ); The use read() to read the image, e.g. example.read(svgFilePath).

Re: [C++ API] Convert svg to png with transparent background

Posted: 2013-05-28T07:42:09-07:00
by henrique
Thanks. This worked :)

Just want to point out (in case anyone has the same problem) that the order in which you do things is important. I tried this solution specifying the PNG format first and then reading the SVG file (which is wrong). The correct order is as follows:

Code: Select all

Magick::Color c(0x0, 0x0, 0x0, 0xFFFF); // transparent
Magick::Image svgImage;
svgImage.backgroundColor(c);
svgImage.read(svgFilePath);  // first read file
svgImage.magick("png");  // then specify PNG format
Magick::Blob blob;
svgImage.write(&blob);
Once again, thanks for the help.