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
	{
		public System.Windows.Forms.TextBox Text1;
		public System.Windows.Forms.Button Command1;
		public System.Windows.Forms.Button Command2;
		private AxIMAGEKIT7Lib.AxImageKit ImageKit1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		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.Text1 = new System.Windows.Forms.TextBox();
			this.Command1 = new System.Windows.Forms.Button();
			this.Command2 = new System.Windows.Forms.Button();
			this.ImageKit1 = new AxIMAGEKIT7Lib.AxImageKit();
			((System.ComponentModel.ISupportInitialize)(this.ImageKit1)).BeginInit();
			this.SuspendLayout();
			// 
			// Text1
			// 
			this.Text1.AcceptsReturn = true;
			this.Text1.AutoSize = false;
			this.Text1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
			this.Text1.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.Text1.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Text1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(0)), ((System.Byte)(0)));
			this.Text1.Location = new System.Drawing.Point(8, 0);
			this.Text1.MaxLength = 0;
			this.Text1.Multiline = true;
			this.Text1.Name = "Text1";
			this.Text1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Text1.Size = new System.Drawing.Size(545, 80);
			this.Text1.TabIndex = 4;
			this.Text1.Text = @"In this sample, the scan driver's user interface is suppressed and we are scanning with a simple user interface built with the ImageKit. Some scan driver's do not allow their interface to be suppressed. Others will but when the interface is suppressed, there are certain scan properties that must be set. Which properties are required varies greatly depending on the make and model of scanner. For more details, please refer to the Scan.Exec method in the help documentation.";
			// 
			// Command1
			// 
			this.Command1.BackColor = System.Drawing.SystemColors.Control;
			this.Command1.Cursor = System.Windows.Forms.Cursors.Default;
			this.Command1.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Command1.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Command1.Location = new System.Drawing.Point(464, 88);
			this.Command1.Name = "Command1";
			this.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Command1.Size = new System.Drawing.Size(89, 41);
			this.Command1.TabIndex = 5;
			this.Command1.Text = "Scan";
			this.Command1.Click += new System.EventHandler(this.Command1_Click);
			// 
			// Command2
			// 
			this.Command2.BackColor = System.Drawing.SystemColors.Control;
			this.Command2.Cursor = System.Windows.Forms.Cursors.Default;
			this.Command2.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Command2.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Command2.Location = new System.Drawing.Point(464, 416);
			this.Command2.Name = "Command2";
			this.Command2.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Command2.Size = new System.Drawing.Size(89, 41);
			this.Command2.TabIndex = 6;
			this.Command2.Text = "End";
			this.Command2.Click += new System.EventHandler(this.Command2_Click);
			// 
			// ImageKit1
			// 
			this.ImageKit1.Location = new System.Drawing.Point(8, 88);
			this.ImageKit1.Name = "ImageKit1";
			this.ImageKit1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("ImageKit1.OcxState")));
			this.ImageKit1.Size = new System.Drawing.Size(449, 385);
			this.ImageKit1.TabIndex = 7;
			this.ImageKit1.AfterScan += new AxIMAGEKIT7Lib._IImageKitEvents_AfterScanEventHandler(this.ImageKit1_AfterScan);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(560, 478);
			this.Controls.Add(this.ImageKit1);
			this.Controls.Add(this.Command2);
			this.Controls.Add(this.Command1);
			this.Controls.Add(this.Text1);
			this.Name = "Form1";
			this.Text = "Getting Started Scanning - Custom UI";
			((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 Command1_Click(object sender, System.EventArgs e)
		{
			bool Ret;
			int[] index = new int[1];
		
			//STEP1: Initialize the TWAIN scan process...
			Ret = ImageKit1.Scan.Initialize(Handle.ToInt32(), 1, 0, "1.00", "NEWTONE ", "ImageKit", "IK7Scan Sample");
			if (Ret == false) 
			{
				MessageBox.Show("Unable to initialize the TWAIN scan process");
			}
			
			//STEP2: Select the scan device...
			// You can list the installed scan devices this way...
			ImageKit1.Scan.Select();
			
			//or if you know the scan device name then you can set it this way..
			//ImageKit1.Scan.DsName = "HP Precisionscan Pro3.1";
			
			//STEP3: Choose the user interface...
			//here we are  suppressing the manufacturer's user interface...
			ImageKit1.Scan.UiMode = IMAGEKIT7Lib.ScanUIModeConstants.ikScanNONUI;
			
			//you must set the scan area...
			ImageKit1.Scan.RectBottom = 11;
			ImageKit1.Scan.RectLeft = 0;
			ImageKit1.Scan.RectRight = 8.5F;
			ImageKit1.Scan.RectTop = 0;
			
			//you should set these other properies as well...
			ImageKit1.Scan.Compression = 0; //this must be set to 0
			ImageKit1.Scan.XResolution = 100; //horizontal resolution
			ImageKit1.Scan.YResolution = 100; //vertical resolution
			
			ImageKit1.Scan.BitDepth = 24; //scan in 24 bit color
			ImageKit1.Scan.Indicator = true; //display the scan progress indicator
			ImageKit1.Scan.PixelType = IMAGEKIT7Lib.ScanPixelTypeConstants.ikScanPixelRGB; //rgb color
			ImageKit1.Scan.UnitMode = IMAGEKIT7Lib.ScanUnitConstants.ikScanInch; //set units in inches
			ImageKit1.Scan.ScanMode = IMAGEKIT7Lib.ScanModeConstants.ikScanDOC; //document plate
			
			//there are other scan properties that can be set but are not listed here.
			//please refer to the help documentation
			
			//STEP4: Execute the scan (note: the index value is a dummy variable)...
			Ret = ImageKit1.Scan.Exec(ref index[0]);
			
			//STEP5: Terminate the TWAIN scan process...
			Ret = ImageKit1.Scan.Terminate();
		}

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

		private void ImageKit1_AfterScan(object sender, AxIMAGEKIT7Lib._IImageKitEvents_AfterScanEvent e)
		{
			//The scanned image data is returned in the DibHandle which is cleared once this event finishes.
			//To save and/or display the scanned image data, make a persistant copy and
			//pass this to the ImageKit Control's ImageHandle then display or save as usual.
			ImageKit1.ImageHandle = ImageKit1.CopyImage(e.dibHandle);
			
			//save the file in bmp format
			ImageKit1.File.FileName = @"C:\test.bmp";
			ImageKit1.File.SaveFile(IMAGEKIT7Lib.SaveFileConstants.ikSaveBMP, 0);
			
			//display the image
			ImageKit1.Display(IMAGEKIT7Lib.DispModeConstants.ikScale);
		}
	}
}
