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

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
henrique
Posts: 2
Joined: 2013-05-24T08:52:12-07:00
Authentication code: 6789

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

Post 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!
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

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

Post 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).
henrique
Posts: 2
Joined: 2013-05-24T08:52:12-07:00
Authentication code: 6789

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

Post 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.
Post Reply