Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / C#
Article

Simple SVG Editor

Rate me:
Please Sign up or sign in to vote.
4.38/5 (18 votes)
18 Sep 2007CPOL1 min read 167.8K   12.6K   92   21
This application is a combination of two projects from The Code Project: DrawTools by Alex Fry and SVGPad by Maurizio Bigoloni

Introduction

SvgPaint is the simplest SVG editor. This application is a combination of two projects from The Code Project: DrawTools by Alex Fry and SVGPad by Maurizio Bigoloni. This application implements only trivial graphics elements from SVG format, no rendering, no transforming, etc.

Sample screenshot

How It Works

Sample screenshot

SvgLib reads and parses the Svg file and creates object SvgDoc with SvgElements.

Then, from SvgElements, create the DrawObjects, objects by DrawObject library.

After editing using the DrawTools library, the graphics objects may be saved in the edited Svg file.

I do not use serialization because it is used in the DrawTools project. I added to DrawObjects static methods that create an XML string with graphic elements in Svg format.

I also added a few graphics classes into used projects:

  • SvgPolyline into SvgPad
  • DrawText, DrawImage into DrawTools

Moreover, I added scaling capability and filling.

How to Transform Svg Elements to Draw Objects

After created SvgDoc, calling recursing method AddFromSvg() (beginning from root element), that, for all Svg elements, calls static method CreateDrawObject() of the DrawObject class:

C#
public void AddFromSvg(SvgElement ele) 
{ 
	while (ele != null) 
	{ 
		DrawObject o = this.CreateDrawObject(ele); 
		if (o != null) 
			Add(o); 
		SvgElement child = ele.getChild(); 
		while (child != null) 
		{ 
			AddFromSvg(child); 
			child = child.getNext(); 
		} 
		ele = ele.getNext(); 
	} 
} 

The method CreateDrawObject() of basic class DrawObject has input parameter SvgElement and calls static method Create() of inherited draw objects that create themselves.

Then, the new draw objects add to GraphicsList object:

C#
DrawObject CreateDrawObject(SvgElement svge) 
{ 
	DrawObject o = null; 
	switch (svge.getElementType()) 
	{ 
		case SvgElement._SvgElementType.typeLine: 
			o = DrawLine.Create((SvgLine )svge); 
			break; 
		case SvgElement._SvgElementType.typeRect: 
			o = DrawRectangle.Create((SvgRect )svge); 
			break; 
		case SvgElement._SvgElementType.typeEllipse: 
			o = DrawEllipse.Create((SvgEllipse )svge); 
			break; 
		case SvgElement._SvgElementType.typePolyline: 
			o = DrawPolygon.Create((SvgPolyline )svge); 
			break; 
		case SvgElement._SvgElementType.typeImage: 
			o = DrawImage.Create((SvgImage )svge); 
			break; 
		case SvgElement._SvgElementType.typeText: 
			o = DrawText.Create((SvgText )svge); 
			break; 
		case SvgElement._SvgElementType.typeGroup: 
			o = CreateGroup((SvgGroup )svge); 
			break; 
		default: 
		break; 
	} 
	return o; 
} 

Graphics Classes

Sample screenshot

For more details, see DrawTools and SVGPad projects on The Code Project site.

I thank Alex Fry and Maurizio Bigoloni for these interesting projects.

History

  • 18th September, 2007: I added print/preview and exported into JPG, GIF, PNG formats
  • 16th November, 2006: Initial version 

Contact Details

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiongreat stuff Pin
Southmountain16-Jun-23 6:18
Southmountain16-Jun-23 6:18 
BugBugs found Pin
Fei Hap Lee8-Apr-14 16:45
Fei Hap Lee8-Apr-14 16:45 
QuestionAlgorithm Pin
Ashwini615-Dec-12 0:52
Ashwini615-Dec-12 0:52 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey20-Feb-12 19:27
professionalManoj Kumar Choubey20-Feb-12 19:27 
GeneralGood work Pin
Fco. Javier Marin17-Oct-08 5:47
Fco. Javier Marin17-Oct-08 5:47 
GeneralNode and Edges Problem Pin
krish_knight32618-Sep-07 22:47
krish_knight32618-Sep-07 22:47 
GeneralRe: Node and Edges Problem Pin
Shokhin20-Sep-07 23:43
Shokhin20-Sep-07 23:43 
General[Message Removed] Pin
nompel1-Oct-08 7:32
nompel1-Oct-08 7:32 
GeneralIn the demo,missed the namespace Pin
sun37855475927-Aug-07 2:45
sun37855475927-Aug-07 2:45 
GeneralRe: In the demo,missed the namespace Pin
Shokhin4-Sep-07 18:24
Shokhin4-Sep-07 18:24 
QuestionI what to Add more shapes? what i have to do Pin
krish_knight32629-Jul-07 23:03
krish_knight32629-Jul-07 23:03 
Hello sir,

I want to add class, circle ,complex rectangle and etc. What i have to do.

krishna

AnswerRe: I what to Add more shapes? what i have to do Pin
Shokhin4-Sep-07 18:29
Shokhin4-Sep-07 18:29 
GeneralForm1.cs(design) is not displaying in .net window Pin
sendnewsletters2-May-07 0:28
sendnewsletters2-May-07 0:28 
GeneralRe: Form1.cs(design) is not displaying in .net window Pin
Shokhin5-May-07 19:35
Shokhin5-May-07 19:35 
GeneralThanks for the subject Pin
domanz11127-Mar-07 6:44
domanz11127-Mar-07 6:44 
GeneralThanks for posting Pin
DougKreutzer7-Mar-07 11:36
DougKreutzer7-Mar-07 11:36 
QuestionScreenShots missing Pin
Andrew Novik29-Nov-06 19:33
Andrew Novik29-Nov-06 19:33 
AnswerRe: ScreenShots missing Pin
Shokhin29-Nov-06 21:30
Shokhin29-Nov-06 21:30 
GeneralSource code missing Pin
Tony Bermudez29-Nov-06 16:29
Tony Bermudez29-Nov-06 16:29 
GeneralRe: Source code missing Pin
Shokhin29-Nov-06 22:00
Shokhin29-Nov-06 22:00 
GeneralRe: Source code missing Pin
Shokhin29-Nov-06 22:03
Shokhin29-Nov-06 22:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.