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 System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private AxIMAGEKIT7Lib.AxImageKit ImageKit1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		int[] x = new int [4];
		int[] y = new int [4];
		int OriginalImageHandle = 0;

		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.textBox1 = new System.Windows.Forms.TextBox();
			this.ImageKit1 = new AxIMAGEKIT7Lib.AxImageKit();
			this.button1 = new System.Windows.Forms.Button();
			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();
			this.SuspendLayout();
			// 
			// 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, 0);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(608, 64);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = @"The SelectImage method allows you to copy a specific area on an image and is the first step in ""regional"" image processing. This sample gets you started using the SelectImage method. Because the RectDraw property is set to True, you can use your mouse to select a rectangle on the image. These coordinates are passed to the SelectImage method and that area is copied and displayed. The original image handle is stored in a variable so that the original image can be redisplayed.";
			// 
			// 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(488, 384);
			this.ImageKit1.TabIndex = 1;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(504, 80);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(112, 40);
			this.button1.TabIndex = 2;
			this.button1.Text = "Load Image";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(504, 400);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(112, 40);
			this.button2.TabIndex = 3;
			this.button2.Text = "End";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(504, 136);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(112, 40);
			this.button3.TabIndex = 4;
			this.button3.Text = "Select And Display";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(504, 224);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(112, 40);
			this.button4.TabIndex = 5;
			this.button4.Text = "Redisplay Original";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(624, 462);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.ImageKit1);
			this.Controls.Add(this.textBox1);
			this.Name = "Form1";
			this.Text = "SelectImage Method";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.ImageKit1)).EndInit();
			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)
		{
			
			//Free the image data that is referred to by the
			//OriginalImageHandle variable. This must be done manually
			if (OriginalImageHandle != 0) 
			{
				ImageKit1.FreeMemory (OriginalImageHandle);
				OriginalImageHandle = 0;
			}
    
			
			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 in actual size display mode...
			ImageKit1.ScrollBar = true;
			ImageKit1.Display(IMAGEKIT7Lib.DispModeConstants.ikActualSize);

			//store the original image data so it is not overwritten...
			OriginalImageHandle = ImageKit1.CopyImage(ImageKit1.ImageHandle);
		}

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

		private void button3_Click(object sender, System.EventArgs e)
		{
			bool ret;
		
		//check to make sure an area has been selected with the mouse...
			if (ImageKit1.RectRight == 0) 
			{
				MessageBox.Show("Please use your mouse to select an area on the image.");
				return;
			}
            //set the select mode...
            ImageKit1.Effect.SelectMode = IMAGEKIT7Lib.AreaSelectModeConstants.ikEffectPolygon;

            //pass the selected area into the x and y coordinate arrays...
            x[0] = ImageKit1.RectLeft;
			y[0] = ImageKit1.RectTop;
            x[1] = ImageKit1.RectRight;
			y[1] = ImageKit1.RectTop;
            x[2] = ImageKit1.RectRight;
			y[2] = ImageKit1.RectBottom;
            x[3] = ImageKit1.RectLeft;
			y[3] = ImageKit1.RectBottom;
            
	        //execute the SelectImage method...
            ret = ImageKit1.Effect.SelectImage(ref x[0], ref y[0], 4, 128, 128, 128);
 
            //reset mouse coordinates...
			ImageKit1.RectLeft = 0;
			ImageKit1.RectTop = 0;
			ImageKit1.RectRight = 0;
			ImageKit1.RectBottom = 0;
		
			ImageKit1.CtlRefresh();
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			//redisplay the original image data...
			ImageKit1.ImageHandle = OriginalImageHandle;
			ImageKit1.CtlRefresh();
		
			//store the original image data again so it is not overwritten...
			OriginalImageHandle = ImageKit1.CopyImage(ImageKit1.ImageHandle);
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			//allow the mouse to be used to select an area on the image...
			ImageKit1.RectDraw = true;
		}
	}
}
