Click here to Skip to main content
Email Password   helpLost your password?

SvgNet & SvgGdi

SvgNet is a C# library designed to facilitate the processing and particularly the creation of SVG documents in C#.
The project homepage is here. Source on the homepage may be more recent than The Code Project copy.

What it does

SvgNet consists of the following parts:

Classes to represent SVG elements and SVG types

SvgNet contains a family of classes that represent SVG elements (e.g. ellipse or tspan) and SVG types (e.g. a transformation list, a style, or a path description). The SVG types are provided with operators to make constructing and modifying SVG documents easy. For example, to change some properties of a style you can do:
myEllipse.Style = new Style("fill:red");
myEllipse.Style.Set("opacity", 0.5);
myEllipse.Style += "stroke:black";
        

Classes to read, write and copy SVG scenes

To get an SVG string from a tree of SVG elements you have created is as simple as this:
SvgElement myRootElement = SvgFactory.ReadSvgString(s);
s = myRootElement.WriteSvgString(); 
XML output can use entities to reduce the size and increase the legibility of the document.

The SvgFactory class also contains methods to make a deep copy of any SVG node and its descendants. All SVG types and elements are cloneable.

The Svg-Gdi bridge

The SvgGdi bridge is a set of classes that use SvgNet to translate between SVG and GDI+. What this means is that any code that uses GDI+ to draw graphics can easily output SVG as well, simply by plugging in the SvgGraphics object. This object is exactly the same as a regular .NET Graphics object, but creates an SVG tree. Even things like hatched fills and line anchors are implemented. The irritatingly old-fashioned 'current transformation' system of GDI+ is implemented by creating nested SVG group elements, resulting in SVG output that's a bit cleaner than the corresponding GDI+ metafile in some ways.

SvgGdi is the main use for SvgNet, although other things (a collection of basic shapes, maybe charting tools) may be built on SvgNet in the future.

Documentation

The SvgNet class libraries are documented in this help package. Additionally, there are two example applications bundled with SvgNet:

The SvgNet Project

SvgNet is an Open Source project under a BSD-like license (license terms are reproduced in every SvgNet source file). SvgNet is copyright 2003 RiskCare Ltd.

The SvgNet project home page is here.
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralPlease provide the sample code... to deal with the file having dynamic element entries.. using XmlAttributeCollection...using C#
Nikkiee
13:04 25 Jun '09  
My requirement is copy the binary data (Images) in base64 form ( from one file, say File B) and paste it to another file say File A.

Here is my FileA looks like ..
<BaseNode >
<rect x="352" y="201.715" fill="none" width="101.429" height="12.143"/>
<text id="XMLID_7_" X="352" Y="210" font-family="'MyriadPro-Regular'" font-size="12">Value1</text>

<image overflow="visible" width="144" height="44" id="XMLID_10_" xlink:href="data:image/jpeg;base64, /9j//...
</image>

<image overflow="visible" width="144" height="44" id="XMLID_11_" xlink:href="data:image/jpeg;base64, /9j//...
</image>

<image overflow="visible" width="144" height="44" id="XMLID_12_" xlink:href="data:image/jpeg;base64, /9j//...
</image>

</BaseNode>

I am picking the image data from File B and pasting it to File A( given above)

So I have written the code like.

string



valueImagePortrait = fileB.SelectSingleNode("//CustomerData//BinaryData//PortraitFile").InnerText;
string valueImageSign = fileB.SelectSingleNode("//CustomerData//BinaryData//SignatureFile ").InnerText;
string valueImageBG = fileB.SelectSingleNode("//CustomerData//BinaryData//BGFile ").InnerText;

XmlNodeList nodeToChangeImage = fileA.GetElementsByTagName(



"image");
for (int i = 0; i < nodeToChangeImage.Count; i++){
XmlAttributeCollection curAttributes1 = nodeToChangeImage[0].Attributes;
curAttributes1.GetNamedItem["xlink:href"].InnerText = "data:image/jpeg;base64,"+ valueImageBG;
XmlAttributeCollection curAttributes2 = nodeToChangeImage[1].Attributes;

curAttributes2["xlink:href"].InnerText = "data:image/jpeg;base64,"+valueImagePortrait;

XmlAttributeCollection curAttributes3 = nodeToChangeImage[2].Attributes;
curAttributes3["xlink:href"].InnerText = "data:image/jpeg;base64,"+ valueImageSign;
}

This is not a perfect code. I am very new to adapt all the XML Apis. Actually this code works fine.
If there are fixed three nos. of <image> nodes in the file A.

But these node are quite, they can be two, one , or more <image> node in file A.

In that situation program crashes.

Can you please help me to provide the optimized sample code to deal with this, using C# for this.

Thanks
Generalremove and insert the another attribute
Nikkiee
13:53 20 Jun '09  
I just want read the value of x and y   from the <<rect>>
and remove the transform attribute from <<text>>
and place the earlier read xand y as a x and y atribute to <<text>>

can some body help me out with a sample code for that.
below is a sample format which i have to modify.


OR read the last two value from the transform attribute ::transform="matrix(1 0 0 1 100.1431 112.2344)"
and delete the transform attribute and add the two bold values and x and y attributes.


<<rect x="100.143" y="103.714" style="fill:none;" width="87.857" height="12.143"/>>;
<text transform="matrix(1 0 0 1 100.1431 112.2344)" style="font-family:'MyriadPro-Regular'; font-size:12;"&gt;Name;/text>>;
GeneralStack Overflow on complex pages
Franz Fiala
21:45 12 Apr '09  
I am using the SVG-library with success. But when the graphics become more complex*) the server hangs and the debugger says "stack overflow error" in function GetXML().

When switching off parts of the drawing, the library works.

Does anyone know how this error can be corrected?

Thank You, Franz

*) The drawing consists of up to 6 charts with about 100 vertical lines. The amount of lines is the problem. When the lines are omited, the drawing works and also when then amount of charts per drawing is reduced to 4.
GeneralIncompatible output, not standard svg
valeriob
2:24 24 Nov '08  
Hello, i'm trying to make this work, so i wrote:


SvgNet.SvgGdi.SvgGraphics g= new SvgNet.SvgGdi.SvgGraphics();

g.FillEllipse(brush, new Rectangle(0, 0, 50, 50));
Pen pen = new Pen(Color.Gray, 10);
g.DrawEllipse(pen, 5, 5, _imageSize.Width - 10, _imageSize.Height - 10);

StreamWriter sw = new StreamWriter("file.svg");
sw.Write( g.WriteSVGString() );

well the output is'nt rendered by firefoxf or any other svg viewer, any idea how to make compatible output? Big Grin


br mode="hold" />

]>

GeneralAnybody can put an example to read a SVG file with this library?
Marcosjgr
8:47 12 Sep '08  
Hi again.
I'm trying to know how to read SVG files with this library, but please, anybody can post an example to read SVG file?
example:
SvgElement ele = new SvgElement();
ele.read("path to svg file");
ele.show();

Anybody can teach me how to do it (read svg files and show them)?
Thanks. Smile
GeneralRe: Anybody can put an example to read a SVG file with this library?
Nikkiee
16:16 10 Jun '09  
You cant read the .SVG file using this library. That portion is not written in this library.
If you really want to do that you have to write you own piece of code. To read the code to reading the .SVG.
I read to do it I was able to do but struck while reading the matrix transformation.
If you would able to write it or get the help somewhere , please le me knw too.

Thanks,
GeneralRe: Anybody can put an example to read a SVG file with this library?
Nikkiee
16:19 10 Jun '09  
not sure how much Mr. Ben can help in this context as it seems above material is just inspired from the site.
http://www.jbrowse.com/svgnet/[^]
AnswerRe: Anybody can put an example to read a SVG file with this library?
Marcosjgr
8:34 15 Jun '09  
Hi Nikkie.
I wrote my own code to read the svg file.
I used an algorithm to read a simple xml file, you can see it here http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTutMellli21.aspx[^]
in the sample number 2 you will see how it works with xmlelements. I use a GoDiagram library to draw the differents figures, for example, if xmlelement == "line" so draw.line(coorx, coory), the coordinates in svg document are the attributes, so you can read the coordinates too.
So you can read the svg file and you can draw the same figure with c#. You can draw without GoDiagram too, only you need to know how to draw lines, circles or wathever in c#.

Have a nice day. Smile
Marcosjgr.
GeneralRe: Anybody can put an example to read a SVG file with this library?
Nikkiee
13:47 20 Jun '09  
HOw you are dealing with the transform matrix,
I am struck with that portion can you help.
How you are reading the 6 cordinates of the attribute transform=matrix(1,2,3,4,5,6)
GeneralWarnings and Excepetions, please Help.
Marcosjgr
16:30 8 Sep '08  
Hi everybody.
I have Vista SP1 and VS 2008 in my pc. Of course, now I downloaded SVG Adobe too and SVG version 0.92.

I'm trying to compile this project but some errors occurs.
My firts question is about the windows, maybe this project does not run here and maybe does not run with VS 2008 too. So if it runs please tell me what I need to do.
Second.
About the errors: First I Build the solution in VS2008 no errors happends but 708 warnings appears, here is one:

Warning	1	Missing XML comment for publicly visible type or member 'SvgNet.SvgElement._children'	C:\Users\M\Documents\Proyectos\svgnet_src092\SvgNet\SvgNet\SvgElement.cs	59	23	SvgNet

Second. I select SvgDocTest as startup project so when I run the project a exceptions happends:
((System.ComponentModel.ISupportInitialize)(this.svgOut)).EndInit()

Here is the part of the code that the project stop, and the exception:

COMException was unhandled
Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))

If anybody know how to resolved, please post it Confused .

Thanks Wink .

Marcos G.
GeneralRe: Warnings and Excepetions, please Help.
Marcosjgr
8:38 12 Sep '08  
I reply myself. Well for all folks that use Vista, take care, because the adobe plug in does not working fine in this O.S.
Use XP to compile this project.

QuestionSvgAElement
aliov_85
1:04 7 Nov '07  
Hi all, how can I use the SvgAElement adding a link to an element?


AnswerRe: SvgAElement
aliov_85
6:44 19 Nov '07  
At first be sure that you are using 092 version of SVGNet, and after here is an example:

SvgAElement link = new SvgAElement("BrifCahiers.aspx");
link.Href = "xlink:href=\"BrifCahiers.aspx\" ";

SvgTextElement text = new SvgTextElement("Textastic!", 3, 400);

link.AddChild(text);
root.AddChild(link);

"BrifCahiers.aspx" is the target file for the link
QuestionAsp.net 2.0 and SVGNet _VERY URGENT_PLEASE
aliov_85
23:23 16 Oct '07  
Hello every body,

Actually I'm trying to run a simple example from SvgDocTest.

But nothing is displayed at running the page.

Here is the code:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// SvgElement myRootElement = new SvgElement();
//
// SvgLineElement myLine = new SvgLineElement(50, 130, 850, 130);
// SvgTextElement myTitle = new SvgTextElement("Enculé", 50, 120);
// myRootElement.AddChild(myLine);
// myRootElement.AddChild(myTitle);
SvgSvgElement root = new SvgSvgElement("4in", "4in", "0,0 100,100");


//adding multiple children

root.AddChild(
new SvgRectElement(5, 5, 5, 5)/*,
new SvgEllipseElement(30, 10, 8, 12),
new SvgTextElement("Textastic!", 3, 20)*/
);
}
}


Any help will be apreciated

Thanks
AliSigh

AnswerRe: Asp.net 2.0 and SVGNet _VERY URGENT_PLEASE
aliov_85
6:39 19 Nov '07  
Should add these lines in order to work.

string s = root.WriteSVGString(true,true);
Response.ContentType = "image/svg+xml";
Response.Write(s);
Generalerror in svgCtl.SRC = "c:\\temp\\foo.svg"
Razik
23:37 3 Apr '07  
error in svgCtl.SRC = "c:\\temp\\foo.svg"

svgCtl.SRC = "c:\\temp\\foo.svg"

while running the sample the above statement in SvgGdiTest project
gives "Object reference not set to an instance of an object" error.
I am using IDE vs2003 and .Net Framework v1.1.
Kindly help.

Thanks
Razik
GeneralNew Home Page Location
logan1337
16:29 3 Mar '07  
FYI, I found the relocated home page for this project:

http://www.jbrowse.com/svgnet/
GeneralRe: New Home Page Location
alreadyused
10:52 5 Aug '08  
that link's dead today, found the home page here:
[^]
but not a lot on it though...
GeneralGood
norm .net
2:10 28 Feb '07  
Need a scalable vector graphics engine for drawing network clouds, just the ticket 5/5!


We made the buttons on the screen look so good you'll want to lick them. Steve Jobs

QuestionStill alive?
Razputin
7:17 5 Feb '07  
Found this great lookin library whilst on my searches, I'm thinking of writing a .NET Framework Extension for SVG and just wanted to check whats been done.
This article was posted a while ago whats the state of play these days? Ben you still about? tried following the links but they're dead.

Cheers.
AnswerRe: Still alive?
Ben Peterson
7:25 5 Feb '07  

I'm still alive and this library still works for rendering GDI+ calls to SVG -- but it's unlikely I'll ever do any more work on it. Thanks for your interest though Smile


URL: http://www.jbrowse.com
Favorite Toy: http://www.ruby-lang.org

GeneralRe: Still alive?
apper
1:55 28 Sep '08  
Is it possible that you create a sourceforge project for this? So maybe other people can join the project and develop it further? E.g. for me it's useless only because it doesn't support FillPath/DrawPath, but maybe sometime I will do something on it and then it would be bad, if I couldn't easily provide it to the community. It is a really great project!
Generali don't know how to install the source file...:(
henin
1:33 18 Oct '06  
hello~everyone~~
i am just beginning to learn SVG...
just as title...Confused
i downloaded the source file,but i don't know how to install the source file...
could you tell me how to do?Cry
thanks ~!!!Blush
GeneralASP.NET + SvgNet??
Micke123
3:41 16 Oct '06  
Hello.
We are som students trying to use SvgNet in asp.net to generate a svg picture´.. But it isnt working. we are using an Embed tag inside the asp.net page like this:

<embed src="SvgTest.aspx" type="image/svg+xml" ginspage="http://www.adobe.com/svg/viewer/install/" />

Below, the code for page load method of SvgTest.aspx.cs

Response.Clear();
SvgSvgElement root = new SvgSvgElement("4in", "4in", "0,0 100,100");


root.AddChildren(
new SvgRectElement(5, 5, 10, 10),
new SvgEllipseElement(30, 10, 8, 12),
new SvgTextElement("PINGU!", 10, 40)
);
string s = root.WriteSVGString(true);
Response.ContentType = "image/svg+xml";
Response.Write(s);

Maybe someone can tell us how this actuallly work??

Regards, micke.Confused
QuestionCompiler Errors
.:MaryAnne:.
19:33 22 Aug '06  
Hi,

im new to learning svg. i open the project svgdoctest. im trying to run the SvgDocTest but compiler is giving many errors. ive already installed adobe svg viewer. below are the errors given:-

a) line 0 = missing file specification for 'lr' command-line option(CS2005)
b) line 33,34,35 = the type/namespace name 'SvgNet' could not be found(are
you missing directive or an assembly reference?)(CS0246)
c) line 50,51 = the type/namespace name 'AxSVGACTIVEXLib' could not be found
(are you missing directive or an assembly reference?)(CS0246)
d) line 220 = the type/namespace name 'SvgElement' could not be found
(are you missing directive or an assembly reference?)(CS0246)
e) line 67 = TODO Add any constuctor code after InitializeComponent call

please help me solve this as im doing a project regarding svgnet. Smile


.:MaryAnne:.


Last Updated 9 Mar 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010