SVG rendering

#Hello
I am trying to port [This Importer][1] across to monogame, yet it has stumped me how to render it. Any help on this or alternative means of rendering svgs without using the xnb would be helpful
[1]: http://wam-games.com/2010/04/svg-in-xna/ “SVG importer for XNA”

I modified this https://github.com/vvvv/SVG which is from the same codebase you use and use to render SVG files to Texture2D.
Have to render SVG to Bitmap first;

        private Bitmap SVGToBitmap(Stream svgStream)
    {
        GdiGraphicsRenderer renderer = new GdiGraphicsRenderer();
        RasterWindow rw = new RasterWindow(480, 360, renderer);
        renderer.Window = rw;
        SvgDocument doc = new SvgDocument(rw);
        try
        {
            doc.Load(svgStream);
            ISvgRect view = doc.RootElement.Viewport;
            XmlNodeList lst = doc.GetElementsByTagName("text");
            foreach (XmlNode node in lst)
            {
                if (node is ISvgTextElement)
                {
                    ISvgTextElement itext = (ISvgTextElement)node;
                    itext.SetAttribute("y", "0");
                    itext.SetAttribute("x", "0");
                }
            }
            double height = Math.Max(view.Height, 1d);
            double width = Math.Max(view.Width, 1d);
            rw.Resize((int)width, (int)height);
            renderer.Render(doc);
        }
        catch (Exception)
        {
            return new Bitmap(1,1);
        }
        return renderer.RasterImage;
    }

You can ignore the loop in the middle for text nodes as that is a “fix” for the odd SVG files I usually get.
You then convert Bitmao to Texture2D;

        private static Texture2D BitmapToTexture2D(
       GraphicsDevice GraphicsDevice,
       System.Drawing.Bitmap image)
    {
        // Buffer size is size of color array multiplied by 4 because    
        // each pixel has four color bytes   
        int bufferSize = image.Height * image.Width * 4;
        // Create new memory stream and save image to stream so    
        // we don't have to save and read file   
        System.IO.MemoryStream memoryStream =
            new System.IO.MemoryStream(bufferSize);
        image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
        // Creates a texture from IO.Stream - our memory stream   
        Texture2D texture = Texture2D.FromStream(
            GraphicsDevice, memoryStream);
        return texture;
    }

What are the usage rights for this? If you don’t intend to impose any, then it would be awesome if you could declare this public domain, or put some kind of license on it at least.

As stated in the linked repo in the Readme. MD, it’s under the MS-PL. Just like the repo he forked from.

Woops! I missed the github link entirely. I must’ve gotten this thread confused with another one before I replied or something.

Np.
Love and peace… :wink:

1 Like