This project has moved and is read-only. For the latest updates, please go here.

svg to png - transparent background

Nov 20, 2013 at 8:13 AM
Edited Nov 20, 2013 at 8:15 AM
I have an svg. I want to create a png with c#.
It works fine but the only problem is transparency. The created png has always a white background.
If I open the original svg on a browser or in inkescape i see a transparent background.
Don't know if i must explicit transparency on svg source(in a specific way) or if i must use a particolar option in Magick.net library.

Thanks in advance for any tip.

This is my function:
private Stream convertSVG(Stream svgAsStream)
            {
                Stream convertedImage = new MemoryStream();

                using (MagickImage MGKimage = new MagickImage())
                {
                    //MGKimage.ColorAlpha(new MagickColor(255, 255, 255)); //alpha on white
                    //MGKimage.Alpha(AlphaOption.Background);

                    //set input image proprieties: SVG
                    MagickReadSettings mrs = new MagickReadSettings();
                    mrs.Format = MagickFormat.Svg;
                    mrs.ColorSpace = ColorSpace.Transparent;
                    
                    MGKimage.Read(svgAsStream, mrs);

                    //set output image proprieties: PNG
                    MGKimage.Format = MagickFormat.Png;
                    MGKimage.BackgroundColor = new MagickColor(System.Drawing.Color.Transparent);

                    MGKimage.Write(convertedImage);
                }

                return convertedImage;
            }
Nov 20, 2013 at 8:37 AM
Edited Nov 20, 2013 at 8:37 AM
The internal support for SVG is not that great in ImageMagick. You can however force Magick.NET to use Inkscape instead. You can use the delegates.xml file for this. I don't have time to post an example now so feel free to figure this out yourself. Setting the ColorSpace to ColorSpace.Transparent will not solve your problem. You can use MagickColor.Transparent for a transparent color.
Nov 20, 2013 at 8:59 AM
Thanks for the reply.
I use the above code on an asp.net page, on the web server, I don't have Inkescape installed.

Is there any way to "translate" the following switch commands of imagemagick to magick.net library?

--THIS--

convert +antialias -background transparent media-playback-start.svg media-playback-start.png;

found that command on this blog.


-- OR THIS--

convert -background none folder.svg folder.png

found that command on this blog
Nov 20, 2013 at 9:29 AM
-background is the BackgroundColor property of MagickImage. This might work:
MGKimage.BackgroundColor = MagickColor.Transparent;
MGKimage.Read(svgAsStream, mrs);
If this doesn't work I might have to add a BackgroundColor property to MagickReadSettings. Can you post a link to your pdf or contact me through codeplex if this doesn't work?
Nov 20, 2013 at 9:47 AM
Alredy tryed this with no luck(as i write above):

MGKimage.BackgroundColor = new MagickColor(System.Drawing.Color.Transparent);

I think is the same as you suggest(or not?).
With "Can you post a link to your pdf" did you mean "your svg"?
Nov 20, 2013 at 9:59 AM
You should try setting the BackgroundColor before reading the image. And yes I mean your svg.
Nov 20, 2013 at 10:40 AM
I'll do the test as soon as possible, if it fails, i'll post the svg, thanks.
Nov 20, 2013 at 1:51 PM
It works! Thanks a lot dlemstra! :)
Dec 12, 2013 at 8:12 AM
hi dlemstra,

i am trying to make the transparent background while converting an image from .tiff to .jpg, using Imagemagic, but it doesn't work for me.

converting tiff to .png with transparent background is working well.

i am using MGKimage.BackgroundColor = MagickColor.Transparent; as per your suggestion.

thanks
Dec 12, 2013 at 9:09 AM
Can you define what you mean by 'it doesn't work for me'? And are you aware of the fact that jpg doesn't support transparency.
Dec 12, 2013 at 9:51 AM
"it doesn't work for me" means if i take a tiff image with black background and try to change it in jpg with transparent background it create jpg with same black background.

image file converted in png format are quite large in size do we have a way to reduce it's size?
Dec 12, 2013 at 9:59 AM
If you want a small jpg file you should choose a background color (e.g. Color.White). If you want to keep the transparency but get a small png file you can find some tips here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=22110
Dec 12, 2013 at 10:25 AM
Thanks dlemstra.

actually i need transparent background image, png is ok for me.

do we have any method to create reduced size png in c#?
or only the way to run the command line argument as you have suggested the link.
Dec 12, 2013 at 12:06 PM
You should 'translate' the command line arguments to methods/properties. For example '-colors 255' is this:
MGKimage.QuantizeColors=255
Dec 12, 2013 at 12:07 PM
Trying to make white background for an tif image, but getting the jpg with same black background as tiff.

using the below code:

using (MagickImage image = new MagickImage())
                        {
                            image.BackgroundColor =System.Drawing.Color.White;
                            image.Read(filePath);
                            image.Format = MagickFormat.Jpg;
                            image.Write(convertDir + "\\" + file + ".jpg");
                        }
what's going wrong here?
Dec 12, 2013 at 1:15 PM
Have you tried setting the background after the read operation?
Dec 12, 2013 at 1:57 PM
Yes, But it's not working.