Change the style dynamically from C#

Apr 1, 2010 at 9:29 AM

Hi,

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

A

Coordinator
Apr 5, 2010 at 1:57 PM

Hey,

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.

Cheers,
Scriv

Apr 20, 2010 at 3: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 0.5.2.0 version.

Do I have a wrong DLL.

Best Regards

Atilla

Coordinator
May 5, 2010 at 11: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.

 

Coordinator
May 6, 2010 at 11:12 AM

Okay, I've given this a go (using the latest source code, but version 0.5.2.0 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.

Scriv

May 7, 2010 at 7:28 PM

Thank you very much it helped a lot.