Change the style dynamically from C#

Apr 1, 2010 at 8:29 AM


I have a SVG file in which the color is coded inside the style section with fill:rgb(X,X,X),

I would like to access this information and convert it to what I want in C# before dumping the SVG into an IMAGE.

I tried

doc.GetElementbyId("style").content = "fill:rgb(Y,Y,Y)";

but this did not work.

Help is appriciated.

Best Regards


Apr 5, 2010 at 12:57 PM


Sorry for not getting back to you sooner.

What you want should be possible by doing the following:

((SvgVisualElement)doc.GetElementById("style")).Fill = new SvgColourServer(Color.Black);

Hope this helps.


Apr 20, 2010 at 2:49 PM

Hi Dave,

Thanks for the answer. I have a problem. I can not find the SvgVisualElement object in my SVG dll. I had version.

Do I have a wrong DLL.

Best Regards


May 5, 2010 at 10:30 PM

Sorry for the extremely late response, Atilla.

It may have been called SvgGraphicsElement in that version. I can't remember off the top of my head when it was refactored to be SvgVisualElement.

I will get a code sample up for you tonight or tomorrow night because it's definitely possible to do what you want to do, I just haven't documented it.

Again, I'm very sorry about not getting back to you sooner.


May 6, 2010 at 10:12 AM

Okay, I've given this a go (using the latest source code, but version should work the same way):

            var sampleDoc = SvgDocument.Open(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\sample.svg"));
            sampleDoc.GetElementById<SvgUse>("Commonwealth_Star").Fill = new SvgColourServer(Color.Black);
            sampleDoc.Draw().Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\sample.png"));

It's simply a case of getting the required element (SvgCircle, SvgPath, SvgPolygon etc) and setting he Fill property of the element and that calling Draw().

I've added this to the Samples folder in the latest check-in under the RuntimeUpdates folder so you can download and run the code (note that it depends on VS 2010 but should be an easy conversion to VS 2008).

Let me know if this isn't suitable for you.


May 7, 2010 at 6:28 PM

Thank you very much it helped a lot.