Click here to Skip to main content
15,897,371 members
Articles / Web Development / ASP.NET

Image Annotation/Markup on the Web using ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.83/5 (17 votes)
5 Oct 20043 min read 99.4K   2.2K   70  
How to perform simple web image annotation using ASP.NET.
using System;
using System.Drawing;

namespace ImageMarkup
{
	/// <summary>
	/// Summary description for MarkType.
	/// </summary>
	public interface MarkableType
	{
		void LoadFromString(string str);
		string SaveAsString();
	}

	public class CircleMark : MarkableType
	{
		private int _radius;
		
		public int Radius
		{
			get { return _radius; }
		}

		public CircleMark(int radius)
		{
			_radius = radius;
		}

		public void LoadFromString(string str)
		{
			try
			{
				_radius = int.Parse(str);
			}
			catch
			{
				_radius = 3;
			}
		}

		public string SaveAsString()
		{
			return _radius.ToString();
		}
	}

	public class RectangleMark : MarkableType
	{
		private int _width;
		private int _height;
		
		public int Width
		{
			get { return _width; }
		}

		public int Height
		{
			get { return _height; }
		}

		public RectangleMark(int width, int height)
		{
			_width = width;
			_height = height;
		}

		public void LoadFromString(string str)
		{
			string[] tmp = str.Split(',');
			try
			{
				_width = int.Parse(tmp[0]);
				_height = int.Parse(tmp[1]);
			}
			catch
			{
				_width = 3;
				_height = 3;
			}
		}

		public string SaveAsString()
		{
			return _width.ToString() + "," + _height.ToString();
		}
	}

	public class ImageMark : MarkableType
	{
		private string _fspath;
		
		public Bitmap Image
		{
			get {
					string path = System.Web.HttpContext.Current.Server.MapPath(_fspath);

					if (System.IO.File.Exists(path))
					{
						return new Bitmap(path);
					}
					else
					{
						return null;
					}
				}
		}

		public ImageMark(string path)
		{
			_fspath = path;
		}

		public void LoadFromString(string str)
		{
			_fspath = str;
		}

		public string SaveAsString()
		{
			return _fspath;
		}
	}

	public class TextMark : MarkableType
	{
		private string _text;

		public string Text
		{
			get { return _text; }
		}
		
		public TextMark(string text)
		{
			_text = text;
		}

		public void LoadFromString(string str)
		{
			_text = str;
		}

		public string SaveAsString()
		{
			return _text;
		}
	}

	public class MarkManager
	{
		public static MarkableType CreateByID(string typeid)
		{
			switch(typeid)
			{
				case "0":
					return new RectangleMark(8, 8);
				case "1":
					return new CircleMark(3);
				case "2":
					return new TextMark("TEXT");
				case "3":
					return new ImageMark("plus.bmp");
				default:
					return null;
			}
		}

		public static void Draw(Graphics g, int x, int y, MarkableType mark)
		{
			if (mark is RectangleMark)
			{
				Draw(g, x, y, (RectangleMark) mark);
			}
			else if (mark is CircleMark)
			{
				Draw(g, x, y, (CircleMark) mark);
			}
			else if (mark is TextMark)
			{
				Draw(g, x, y, (TextMark) mark);
			}
			else if (mark is ImageMark)
			{
				Draw(g, x, y, (ImageMark) mark);
			}
		}

		public static void Draw(Graphics g, int x, int y, RectangleMark rect)
		{
			Pen pen = new Pen(Color.Red, 1.5f);
			g.DrawRectangle(pen, x, y, rect.Width, rect.Height);
		}

		public static void Draw(Graphics g, int x, int y, CircleMark circle)
		{
			
			SolidBrush brush = new SolidBrush(Color.Red);
			g.FillEllipse(brush, x, y, circle.Radius*2, circle.Radius*2);
		}

		public static void Draw(Graphics g, int x, int y, TextMark text)
		{
			SolidBrush brush = new SolidBrush(Color.Green);

			string familyName = "Tahoma";

			Font font;
			font = new Font(familyName, 12F, FontStyle.Bold);

			StringFormat format = new StringFormat();
			format.Alignment = StringAlignment.Center;
			format.LineAlignment = StringAlignment.Center;

			g.DrawString(text.Text, font, brush, 
				x, y, format);
		}

		public static void Draw(Graphics g, int x, int y, ImageMark image)
		{
			if (image.Image != null)
			{
				g.DrawImage(image.Image, x, y);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Singapore Singapore
I work in the software service industry. I love to build decision support systems embracing operations research and machine intelligence.

In my spare time I enjoy writing interesting proof-of-concept applications in many flavors of technologies as well as reading AI in game development. I watch many films. Many titles from Criterion Collection are my beloved, from Kurosawa, Tarkovsky, Kubrick, Ozu, film noir classic, you name it.

Presently I stay in Singapore and is a regular in http://www.sgdotnet.org (Singapore DotNet User Group), and I also write from time to time at http://community.sgdotnet.org/blogs/blackinkbottles_ink/default.aspx. Drop by if you can.

Comments and Discussions