using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Project1
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private AxIMAGEKIT7Lib.AxImageKit ImageKit1;
		private AxIMAGEKIT7Lib.AxImageKit ImageKit2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton radioButton1;
		private System.Windows.Forms.RadioButton radioButton2;
		private System.Windows.Forms.RadioButton radioButton3;
		private System.Windows.Forms.RadioButton radioButton4;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;


		private bool rectangle;
		private IMAGEKIT7Lib.AreaSelectModeConstants SMode;
		private int Points;
		private int[] Ix = new int[32];
		private int[] Iy = new int[32];

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.ImageKit1 = new AxIMAGEKIT7Lib.AxImageKit();
			this.ImageKit2 = new AxIMAGEKIT7Lib.AxImageKit();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.radioButton1 = new System.Windows.Forms.RadioButton();
			this.radioButton2 = new System.Windows.Forms.RadioButton();
			this.radioButton3 = new System.Windows.Forms.RadioButton();
			this.radioButton4 = new System.Windows.Forms.RadioButton();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.ImageKit1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.ImageKit2)).BeginInit();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// ImageKit1
			// 
			this.ImageKit1.Location = new System.Drawing.Point(8, 72);
			this.ImageKit1.Name = "ImageKit1";
			this.ImageKit1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("ImageKit1.OcxState")));
			this.ImageKit1.Size = new System.Drawing.Size(305, 225);
			this.ImageKit1.TabIndex = 0;
			this.ImageKit1.MouseDownImage += new AxIMAGEKIT7Lib._IImageKitEvents_MouseDownImageEventHandler(this.ImageKit1_MouseDownImage);
			this.ImageKit1.MouseDownEvent += new AxIMAGEKIT7Lib._IImageKitEvents_MouseDownEventHandler(this.ImageKit1_MouseDownEvent);
			// 
			// ImageKit2
			// 
			this.ImageKit2.Location = new System.Drawing.Point(360, 72);
			this.ImageKit2.Name = "ImageKit2";
			this.ImageKit2.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("ImageKit2.OcxState")));
			this.ImageKit2.Size = new System.Drawing.Size(305, 225);
			this.ImageKit2.TabIndex = 1;
			// 
			// textBox1
			// 
			this.textBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
			this.textBox1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(0)), ((System.Byte)(0)));
			this.textBox1.Location = new System.Drawing.Point(8, 8);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(672, 32);
			this.textBox1.TabIndex = 2;
			this.textBox1.Text = "The SelectImage method can copy the entire image, an area on the image defined by" +
				" a polygon, or an elliptical area. This sample illustrates these ways to select " +
				"portions of an image and copy them with the SelectImage method.";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(128, 48);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(80, 16);
			this.label1.TabIndex = 3;
			this.label1.Text = "Original Image";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(456, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 16);
			this.label2.TabIndex = 4;
			this.label2.Text = "Processed Image";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(16, 320);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(96, 32);
			this.button1.TabIndex = 5;
			this.button1.Text = "Load Image";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.radioButton4);
			this.groupBox1.Controls.Add(this.radioButton3);
			this.groupBox1.Controls.Add(this.radioButton2);
			this.groupBox1.Controls.Add(this.radioButton1);
			this.groupBox1.Location = new System.Drawing.Point(128, 304);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(120, 88);
			this.groupBox1.TabIndex = 6;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "SelectMode";
			// 
			// radioButton1
			// 
			this.radioButton1.Location = new System.Drawing.Point(8, 16);
			this.radioButton1.Name = "radioButton1";
			this.radioButton1.Size = new System.Drawing.Size(88, 16);
			this.radioButton1.TabIndex = 0;
			this.radioButton1.Text = "Total Image";
			this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
			// 
			// radioButton2
			// 
			this.radioButton2.Location = new System.Drawing.Point(8, 32);
			this.radioButton2.Name = "radioButton2";
			this.radioButton2.Size = new System.Drawing.Size(88, 16);
			this.radioButton2.TabIndex = 1;
			this.radioButton2.Text = "Polygon";
			this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
			// 
			// radioButton3
			// 
			this.radioButton3.Location = new System.Drawing.Point(8, 48);
			this.radioButton3.Name = "radioButton3";
			this.radioButton3.Size = new System.Drawing.Size(88, 16);
			this.radioButton3.TabIndex = 2;
			this.radioButton3.Text = "Ellipse";
			this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
			// 
			// radioButton4
			// 
			this.radioButton4.Location = new System.Drawing.Point(8, 64);
			this.radioButton4.Name = "radioButton4";
			this.radioButton4.Size = new System.Drawing.Size(88, 16);
			this.radioButton4.TabIndex = 3;
			this.radioButton4.Text = "Rectangle";
			this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButton4_CheckedChanged);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(272, 320);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(96, 32);
			this.button2.TabIndex = 7;
			this.button2.Text = "Copy Selection";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(272, 360);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(96, 32);
			this.button3.TabIndex = 8;
			this.button3.Text = "Clear Selection";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(576, 352);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(96, 32);
			this.button4.TabIndex = 9;
			this.button4.Text = "End";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(686, 406);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.ImageKit2);
			this.Controls.Add(this.ImageKit1);
			this.Name = "Form1";
			this.Text = "Selecting areas";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.ImageKit1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.ImageKit2)).EndInit();
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		

		private void button1_Click(object sender, System.EventArgs e)
		{
			if (ImageKit1.File.OpenFileDialog() == false) 
				return;			
			
			//load the image file...
			ImageKit1.File.LoadFile(IMAGEKIT7Lib.LoadFileConstants.ikLoad);

			//check to see that the loaded image is a raster image...
			if (ImageKit1.ImageKind != IMAGEKIT7Lib.ImageKindConstants.ikRasterImage) 
			{
				ImageKit1.FreeMemory(ImageKit1.ImageHandle);
				MessageBox.Show("Please load a raster image");
				return;
			}
			
			//display image...
			ImageKit1.Display(IMAGEKIT7Lib.DispModeConstants.ikScale);
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			bool ret;
		
		
		//exit if no image is loaded in the ImageKit1 Control
			if (ImageKit1.ImageHandle == 0) 
			{
				MessageBox.Show("Please load an image");
			}
		
		
		// copy the original image in ImageKit1.ImageHandle into the ImageKit2.ImageHandle
		ImageKit2.ImageHandle = ImageKit1.CopyImage(ImageKit1.ImageHandle);
		
		//process the area selected by mouse
		ImageKit2.Effect.InOut = IMAGEKIT7Lib.InOutSelectModeConstants.ikInside;
		ImageKit2.Effect.SelectMode = SMode;
		
		//if the rectangle option is selected then set the coordinates of the mouse drag rectangle
		//into the coordinate arrays
			if (rectangle == true) 
			{
				Ix[0] = ImageKit1.RectLeft;
				Iy[0] = ImageKit1.RectTop;
				Ix[1] = ImageKit1.RectRight;
				Iy[1] = ImageKit1.RectTop;
				Ix[2] = ImageKit1.RectRight;
				Iy[2] = ImageKit1.RectBottom;
				Ix[3] = ImageKit1.RectLeft;
				Iy[3] = ImageKit1.RectBottom;
				Points = 4;
			}
			else 
			{
				ImageKit2.Effect.RectLeft = ImageKit1.RectLeft;
				ImageKit2.Effect.RectTop = ImageKit1.RectTop;
				ImageKit2.Effect.RectRight = ImageKit1.RectRight;
				ImageKit2.Effect.RectBottom = ImageKit1.RectBottom;
			}
		
		//select that portion of the image defined by the coordinate points
		//(when SMode = ikEffectPolygon) or defined by the RectLeft,RectTop,
		//RectRight,and RectBottom property values (when SMode = ikEffectElipse)
		ret = ImageKit2.Effect.SelectImage(ref Ix[0], ref Iy[0], Points, 255, 255, 255);
			if (ret == false) 
			{
				ImageKit2.Display(IMAGEKIT7Lib.DispModeConstants.ikClear);
				MessageBox.Show("Unable to select the area on the image.");
				return;
			}
		
		Points = 0;		
		ImageKit2.Display(IMAGEKIT7Lib.DispModeConstants.ikScale);
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			//clear the ImageKit2 control
			Points = 0;
			ImageKit1.RectLeft = 0;
			ImageKit1.RectTop = 0;
			ImageKit1.RectRight = 0;
			ImageKit1.RectBottom = 0;
			ImageKit1.RectDraw = false;
			ImageKit1.Display(IMAGEKIT7Lib.DispModeConstants.ikScale);
			SMode = IMAGEKIT7Lib.AreaSelectModeConstants.ikEffectAll;
			radioButton1.Checked = true;
		
			if (ImageKit2.ImageHandle != 0) 
			{
				ImageKit2.FreeMemory(ImageKit2.ImageHandle);
				ImageKit2.ImageHandle = 0;
			}
			ImageKit2.Display(IMAGEKIT7Lib.DispModeConstants.ikScale);
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			Points = 0;
			SMode = IMAGEKIT7Lib.AreaSelectModeConstants.ikEffectAll;
			ImageKit1.RectDraw = false;
			radioButton1.Checked=true;
		}

		private void ImageKit1_MouseDownEvent(object sender, AxIMAGEKIT7Lib._IImageKitEvents_MouseDownEvent e)
		{
			bool ret;
		
			if (rectangle == false) 
			{
				//draw little circles where the mouse is clicked....
				if (ImageKit1.ImageHandle != 0)
				{
					if (SMode == IMAGEKIT7Lib.AreaSelectModeConstants.ikEffectPolygon) 
					{
						ImageKit1.PrintDraw.ClearProperty();
						ImageKit1.PrintDraw.PenMode = IMAGEKIT7Lib.PenModeConstants.ikPenModeNotXorPen;
						ImageKit1.PrintDraw.PenWidth = 2;
						ImageKit1.PrintDraw.PenStyle = IMAGEKIT7Lib.PenStyleConstants.ikPenSolid;
						ret = ImageKit1.PrintDraw.Ellipse(ImageKit1.hDC, e.x - 2, e.y - 2, e.x + 2, e.y + 2, IMAGEKIT7Lib.OutPutDeviceModeConstants.ikScreen);
					}					
				}
			}
		}

		private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
		{
			ImageKit1.RectLeft = 0;
			ImageKit1.RectTop = 0;
			ImageKit1.RectRight = 0;
			ImageKit1.RectBottom = 0;
			Points = 0;
			SMode = IMAGEKIT7Lib.AreaSelectModeConstants.ikEffectAll;
			ImageKit1.RectDraw = false;
			rectangle = false;
		}

		private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
		{
			ImageKit1.RectLeft = 0;
			ImageKit1.RectTop = 0;
			ImageKit1.RectRight = 0;
			ImageKit1.RectBottom = 0;
			Points = 0;
			SMode = IMAGEKIT7Lib.AreaSelectModeConstants.ikEffectPolygon;
			ImageKit1.RectDraw = false;
			rectangle = false;
		}

		private void radioButton3_CheckedChanged(object sender, System.EventArgs e)
		{
			ImageKit1.RectLeft = 0;
			ImageKit1.RectTop = 0;
			ImageKit1.RectRight = 0;
			ImageKit1.RectBottom = 0;
			Points = 0;
			SMode = IMAGEKIT7Lib.AreaSelectModeConstants.ikEffectEllipse;
			ImageKit1.RectDraw = true;
			rectangle = false;
		}
		private void radioButton4_CheckedChanged(object sender, System.EventArgs e)
		{
			
			ImageKit1.RectLeft = 0;
			ImageKit1.RectTop = 0;
			ImageKit1.RectRight = 0;
			ImageKit1.RectBottom = 0;
			Points = 0;
			SMode = IMAGEKIT7Lib.AreaSelectModeConstants.ikEffectPolygon;
			ImageKit1.RectDraw = true;
			rectangle = true;
		}

		private void ImageKit1_MouseDownImage(object sender, AxIMAGEKIT7Lib._IImageKitEvents_MouseDownImageEvent e)
		{
			if (SMode == IMAGEKIT7Lib.AreaSelectModeConstants.ikEffectPolygon) 
			{
				Ix[Points] = e.x;
                Iy[Points] = e.y;
                Points = Points + 1;
			}
                
		}
	}
}
