Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#

Canvas implementation for C#

Rate me:
Please Sign up or sign in to vote.
4.43/5 (29 votes)
21 Mar 20044 min read 255.5K   9.6K   118  
Design and implementation of canvas in C#
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Timers;
using CanvasControl;

namespace CanvasDemo
{
	public class FrameInfo 
	{
		public FrameInfo(int ch,string fn)
		{
			chain = ch;
			fname = fn;
		}
		public int chain;
		public string fname;
	}

	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button openImage;
		
		private System.Windows.Forms.Button btnZoomIn;
		private System.Windows.Forms.Button btnZoomOut;
		private CanvasControl.Canvas canvas1;
		private System.Windows.Forms.HScrollBar hScrollBar1;
		private System.Windows.Forms.VScrollBar vScrollBar1;
		
		private SortedList m_frames;
		private System.Windows.Forms.Button btnNewRect;
		private System.Windows.Forms.Button btnNewPoly;
		private System.Windows.Forms.Button btnSelect;
		private System.Windows.Forms.Button delete;
		private System.Windows.Forms.Button btnCreateLine;
		private System.Windows.Forms.Button btnMove;
	
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox entryFname;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Label label3;
		private CanvasControl.Canvas canvas2;
		private System.Windows.Forms.Button btnColor;
		
		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			
			canvas1.connectScrolls(hScrollBar1,vScrollBar1);
			
			m_frames = new SortedList();
			canvas1.CoordChangeEvent += new CanvasControl.Canvas.CoordChangeEventHandler(OnCoordChainged);
			canvas1.ItemNewEvent += new CanvasControl.Canvas.ItemEventHandler(OnNewCanvasItem);			
			canvas1.ItemEnterEvent += new CanvasControl.Canvas.ItemEventHandler(OnEnterCanvasItem);			

			canvas2.setGeomModel(canvas1.getGeomModel());
		}

		
		public void OnNewCanvasItem(object sender, CanvasControl.ItemEventArgs e)
		{
			textBox2.Text = e.indx.ToString();
		}

		public void OnEnterCanvasItem(object sender, CanvasControl.ItemEventArgs e)
		{
			textBox3.Text = e.indx.ToString();
		}

		public void OnCoordChainged(object sender, CanvasControl.CoordEventArgs e)
		{
			textBox1.Text = e.X.ToString() + "," + e.Y.ToString();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.openImage = new System.Windows.Forms.Button();
			this.btnZoomIn = new System.Windows.Forms.Button();
			this.btnZoomOut = new System.Windows.Forms.Button();
			this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
			this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
			this.entryFname = new System.Windows.Forms.TextBox();
			this.btnNewRect = new System.Windows.Forms.Button();
			this.btnNewPoly = new System.Windows.Forms.Button();
			this.btnSelect = new System.Windows.Forms.Button();
			this.delete = new System.Windows.Forms.Button();
			this.btnCreateLine = new System.Windows.Forms.Button();
			this.btnMove = new System.Windows.Forms.Button();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.canvas1 = new CanvasControl.Canvas();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.btnColor = new System.Windows.Forms.Button();
			this.canvas2 = new CanvasControl.Canvas();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(424, 120);
			this.textBox1.Name = "textBox1";
			this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBox1.Size = new System.Drawing.Size(56, 20);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "0000";
			// 
			// openImage
			// 
			this.openImage.Location = new System.Drawing.Point(8, 48);
			this.openImage.Name = "openImage";
			this.openImage.Size = new System.Drawing.Size(80, 23);
			this.openImage.TabIndex = 6;
			this.openImage.Text = "open image";
			this.openImage.Click += new System.EventHandler(this.openImage_Click);
			// 
			// btnZoomIn
			// 
			this.btnZoomIn.Location = new System.Drawing.Point(192, 80);
			this.btnZoomIn.Name = "btnZoomIn";
			this.btnZoomIn.Size = new System.Drawing.Size(24, 24);
			this.btnZoomIn.TabIndex = 8;
			this.btnZoomIn.Text = "+";
			this.btnZoomIn.Click += new System.EventHandler(this.btnZoomIn_Click);
			// 
			// btnZoomOut
			// 
			this.btnZoomOut.Location = new System.Drawing.Point(216, 80);
			this.btnZoomOut.Name = "btnZoomOut";
			this.btnZoomOut.Size = new System.Drawing.Size(24, 24);
			this.btnZoomOut.TabIndex = 9;
			this.btnZoomOut.Text = "-";
			this.btnZoomOut.Click += new System.EventHandler(this.btnZoomOut_Click);
			// 
			// hScrollBar1
			// 
			this.hScrollBar1.Location = new System.Drawing.Point(8, 384);
			this.hScrollBar1.Name = "hScrollBar1";
			this.hScrollBar1.Size = new System.Drawing.Size(328, 17);
			this.hScrollBar1.TabIndex = 11;
			this.hScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.hScrollBar1_Scroll);
			// 
			// vScrollBar1
			// 
			this.vScrollBar1.Location = new System.Drawing.Point(336, 112);
			this.vScrollBar1.Name = "vScrollBar1";
			this.vScrollBar1.Size = new System.Drawing.Size(16, 272);
			this.vScrollBar1.TabIndex = 12;
			this.vScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.vScrollBar1_Scroll);
			// 
			// entryFname
			// 
			this.entryFname.Location = new System.Drawing.Point(104, 48);
			this.entryFname.Name = "entryFname";
			this.entryFname.Size = new System.Drawing.Size(376, 20);
			this.entryFname.TabIndex = 15;
			this.entryFname.Text = "100";
			// 
			// btnNewRect
			// 
			this.btnNewRect.Location = new System.Drawing.Point(8, 80);
			this.btnNewRect.Name = "btnNewRect";
			this.btnNewRect.Size = new System.Drawing.Size(40, 23);
			this.btnNewRect.TabIndex = 16;
			this.btnNewRect.Text = "rect";
			this.btnNewRect.Click += new System.EventHandler(this.btnNewRect_Click);
			// 
			// btnNewPoly
			// 
			this.btnNewPoly.Location = new System.Drawing.Point(48, 80);
			this.btnNewPoly.Name = "btnNewPoly";
			this.btnNewPoly.Size = new System.Drawing.Size(48, 23);
			this.btnNewPoly.TabIndex = 17;
			this.btnNewPoly.Text = "poly";
			this.btnNewPoly.Click += new System.EventHandler(this.btnNewPoly_Click);
			// 
			// btnSelect
			// 
			this.btnSelect.Location = new System.Drawing.Point(384, 80);
			this.btnSelect.Name = "btnSelect";
			this.btnSelect.Size = new System.Drawing.Size(48, 24);
			this.btnSelect.TabIndex = 18;
			this.btnSelect.Text = "select";
			this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
			// 
			// delete
			// 
			this.delete.Location = new System.Drawing.Point(432, 80);
			this.delete.Name = "delete";
			this.delete.Size = new System.Drawing.Size(48, 24);
			this.delete.TabIndex = 19;
			this.delete.Text = "delete";
			this.delete.Click += new System.EventHandler(this.delete_Click);
			// 
			// btnCreateLine
			// 
			this.btnCreateLine.Location = new System.Drawing.Point(96, 80);
			this.btnCreateLine.Name = "btnCreateLine";
			this.btnCreateLine.Size = new System.Drawing.Size(48, 23);
			this.btnCreateLine.TabIndex = 20;
			this.btnCreateLine.Text = "line";
			this.btnCreateLine.Click += new System.EventHandler(this.btnCreateLine_Click);
			// 
			// btnMove
			// 
			this.btnMove.Location = new System.Drawing.Point(336, 80);
			this.btnMove.Name = "btnMove";
			this.btnMove.Size = new System.Drawing.Size(48, 24);
			this.btnMove.TabIndex = 21;
			this.btnMove.Text = "move";
			this.btnMove.Click += new System.EventHandler(this.btnMove_Click);
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(424, 152);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(56, 20);
			this.textBox2.TabIndex = 22;
			this.textBox2.Text = "0";
			// 
			// canvas1
			// 
			this.canvas1.Location = new System.Drawing.Point(8, 112);
			this.canvas1.Name = "canvas1";
			this.canvas1.Size = new System.Drawing.Size(328, 272);
			this.canvas1.TabIndex = 23;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(352, 120);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 16);
			this.label1.TabIndex = 24;
			this.label1.Text = "coords:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(352, 152);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 23);
			this.label2.TabIndex = 25;
			this.label2.Text = "new item:";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(424, 184);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(56, 20);
			this.textBox3.TabIndex = 26;
			this.textBox3.Text = "0000";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(352, 184);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 23);
			this.label3.TabIndex = 27;
			this.label3.Text = "enter item:";
			// 
			// btnColor
			// 
			this.btnColor.Location = new System.Drawing.Point(264, 80);
			this.btnColor.Name = "btnColor";
			this.btnColor.Size = new System.Drawing.Size(56, 24);
			this.btnColor.TabIndex = 28;
			this.btnColor.Text = "color";
			this.btnColor.Click += new System.EventHandler(this.btnColor_Click);
			// 
			// canvas2
			// 
			this.canvas2.Location = new System.Drawing.Point(352, 256);
			this.canvas2.Name = "canvas2";
			this.canvas2.Size = new System.Drawing.Size(128, 128);
			this.canvas2.TabIndex = 29;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(504, 414);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.canvas2,
																		  this.btnColor,
																		  this.label3,
																		  this.textBox3,
																		  this.label2,
																		  this.label1,
																		  this.canvas1,
																		  this.textBox2,
																		  this.btnMove,
																		  this.btnCreateLine,
																		  this.delete,
																		  this.btnSelect,
																		  this.btnNewPoly,
																		  this.btnNewRect,
																		  this.entryFname,
																		  this.vScrollBar1,
																		  this.hScrollBar1,
																		  this.btnZoomOut,
																		  this.btnZoomIn,
																		  this.openImage,
																		  this.textBox1});
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
	
		private void openImage_Click(object sender, System.EventArgs e)
		{
			
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.ShowDialog();
			string fname = openFileDialog.FileName;
			entryFname.Text = fname;
			canvas1.LoadImage(fname);
			canvas2.fitToWidget();
		}

		private void btnZoomIn_Click(object sender, System.EventArgs e)
		{
			canvas1.ZoomIn();
		}

		
		private void hScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
		{
			textBox1.Text = "VScrollBar Value:(OnScroll Event) " + e.NewValue.ToString();
			canvas1.ScrollHorizontal(e.NewValue);
		}

		private void btnZoomOut_Click(object sender, System.EventArgs e)
		{
			canvas1.ZoomOut();
		}

		private void vScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
		{
			textBox1.Text = "HScrollBar Value:(OnScroll Event) " + e.NewValue.ToString();
			canvas1.ScrollVertical(e.NewValue);
		}

		private void btnNewRect_Click(object sender, System.EventArgs e)
		{
			canvas1.CreateRectangle();
		}

		private void btnSelect_Click(object sender, System.EventArgs e)
		{
			canvas1.StartSelection();
		}

		private void delete_Click(object sender, System.EventArgs e)
		{
			canvas1.DeleteSelected();
		}

		private void btnCreateLine_Click(object sender, System.EventArgs e)
		{
			canvas1.CreateLine();
		}

		private void btnNewPoly_Click(object sender, System.EventArgs e)
		{
			canvas1.CreatePolygon();
		}

		private void btnMove_Click(object sender, System.EventArgs e)
		{
			canvas1.StartMoving();
		}

		private void btnColor_Click(object sender, System.EventArgs e)
		{
			ColorDialog colorDialog = new ColorDialog();	
			if(colorDialog.ShowDialog() != DialogResult.OK)
			{
				return;				
			}

			Color color = colorDialog.Color;
			int [] selected = canvas1.GetSelected();
			foreach(int i in selected)
			{
				CanvasControl.CanvasItem item = canvas1.GetItem(i);
				item.Icolor = color;
				item.select(false);
			}

			canvas1.Invalidate();
			canvas1.Update();

		}

		

		

		
	}
}

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
Canada Canada
Baranovsky Eduard has been a software developer for more then 10 years. He has an experence in image processing, computer graphics and distributed systems design.

Comments and Discussions