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

namespace CSharpOCR
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.CheckBox checkBox1;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.LinkLabel linkLabel1;
		private System.Windows.Forms.Button buttonBrowse;
		private System.Windows.Forms.TextBox textImage;
		private System.Windows.Forms.Button buttonOCR;
		private System.Windows.Forms.Button buttonBarCode;
		private System.Windows.Forms.TextBox textStartX;
		private System.Windows.Forms.TextBox textStartY;
		private System.Windows.Forms.TextBox textWidth;
		private System.Windows.Forms.TextBox textHeight;
		private System.Windows.Forms.Button buttonOCRPart;
		private System.Windows.Forms.TextBox textResults;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.Label label9;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		[DllImport("AspriseOCR.dll")]
		static extern string OCR(string file, int type);

		[DllImport("AspriseOCR.dll")]
		static extern string OCRpart(string file, int type, int startX, int startY, int width, int height);

		[DllImport("AspriseOCR.dll")]
		static extern string OCRBarCodes(string file, int type);
 
		[DllImport("AspriseOCR.dll")]
		static extern string OCRpartBarCodes(string file, int type, int startX, int startY, int width, int height);



		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			openFileDialog1.InitialDirectory = Application.StartupPath;
		}

		/// <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()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.buttonOCRPart = new System.Windows.Forms.Button();
			this.textHeight = new System.Windows.Forms.TextBox();
			this.label7 = new System.Windows.Forms.Label();
			this.textWidth = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.textStartY = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.textStartX = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			this.buttonBarCode = new System.Windows.Forms.Button();
			this.buttonOCR = new System.Windows.Forms.Button();
			this.buttonBrowse = new System.Windows.Forms.Button();
			this.textImage = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.textResults = new System.Windows.Forms.TextBox();
			this.linkLabel1 = new System.Windows.Forms.LinkLabel();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.label9 = new System.Windows.Forms.Label();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.BackColor = System.Drawing.SystemColors.ControlLightLight;
			this.label1.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.ForeColor = System.Drawing.Color.OrangeRed;
			this.label1.Location = new System.Drawing.Point(0, 0);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(672, 40);
			this.label1.TabIndex = 0;
			this.label1.Text = "Welcome to Asprise OCR Demo!";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(0, 40);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(672, 32);
			this.label2.TabIndex = 1;
			this.label2.Text = "Steps: (1) Select the image to be OCRed; (2) Press the \'OCR\' button to start reco" +
				"gnizing.";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.buttonOCRPart);
			this.groupBox1.Controls.Add(this.textHeight);
			this.groupBox1.Controls.Add(this.label7);
			this.groupBox1.Controls.Add(this.textWidth);
			this.groupBox1.Controls.Add(this.label6);
			this.groupBox1.Controls.Add(this.textStartY);
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Controls.Add(this.textStartX);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.checkBox1);
			this.groupBox1.Controls.Add(this.buttonBarCode);
			this.groupBox1.Controls.Add(this.buttonOCR);
			this.groupBox1.Controls.Add(this.buttonBrowse);
			this.groupBox1.Controls.Add(this.textImage);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.label9);
			this.groupBox1.Location = new System.Drawing.Point(8, 80);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(656, 136);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "OCR";
			// 
			// buttonOCRPart
			// 
			this.buttonOCRPart.Location = new System.Drawing.Point(440, 104);
			this.buttonOCRPart.Name = "buttonOCRPart";
			this.buttonOCRPart.Size = new System.Drawing.Size(208, 23);
			this.buttonOCRPart.TabIndex = 14;
			this.buttonOCRPart.Text = "OCR on part of the image";
			this.buttonOCRPart.Click += new System.EventHandler(this.buttonOCRPart_Click);
			// 
			// textHeight
			// 
			this.textHeight.Location = new System.Drawing.Point(376, 104);
			this.textHeight.Name = "textHeight";
			this.textHeight.Size = new System.Drawing.Size(40, 22);
			this.textHeight.TabIndex = 13;
			this.textHeight.Text = "";
			// 
			// label7
			// 
			this.label7.AutoSize = true;
			this.label7.Location = new System.Drawing.Point(328, 104);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(44, 18);
			this.label7.TabIndex = 12;
			this.label7.Text = "Height";
			this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// textWidth
			// 
			this.textWidth.Location = new System.Drawing.Point(272, 104);
			this.textWidth.Name = "textWidth";
			this.textWidth.Size = new System.Drawing.Size(40, 22);
			this.textWidth.TabIndex = 11;
			this.textWidth.Text = "";
			// 
			// label6
			// 
			this.label6.AutoSize = true;
			this.label6.Location = new System.Drawing.Point(224, 104);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(39, 18);
			this.label6.TabIndex = 10;
			this.label6.Text = "Width";
			this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// textStartY
			// 
			this.textStartY.Location = new System.Drawing.Point(168, 104);
			this.textStartY.Name = "textStartY";
			this.textStartY.Size = new System.Drawing.Size(40, 22);
			this.textStartY.TabIndex = 9;
			this.textStartY.Text = "";
			// 
			// label5
			// 
			this.label5.AutoSize = true;
			this.label5.Location = new System.Drawing.Point(120, 104);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(46, 18);
			this.label5.TabIndex = 8;
			this.label5.Text = "StartY:";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// textStartX
			// 
			this.textStartX.Location = new System.Drawing.Point(64, 104);
			this.textStartX.Name = "textStartX";
			this.textStartX.Size = new System.Drawing.Size(40, 22);
			this.textStartX.TabIndex = 7;
			this.textStartX.Text = "";
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Location = new System.Drawing.Point(16, 104);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(46, 18);
			this.label4.TabIndex = 6;
			this.label4.Text = "StartX:";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// checkBox1
			// 
			this.checkBox1.Location = new System.Drawing.Point(16, 72);
			this.checkBox1.Name = "checkBox1";
			this.checkBox1.Size = new System.Drawing.Size(272, 24);
			this.checkBox1.TabIndex = 5;
			this.checkBox1.Text = "Perform OCR on part of the image only";
			this.checkBox1.CheckStateChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
			// 
			// buttonBarCode
			// 
			this.buttonBarCode.Location = new System.Drawing.Point(560, 30);
			this.buttonBarCode.Name = "buttonBarCode";
			this.buttonBarCode.Size = new System.Drawing.Size(88, 23);
			this.buttonBarCode.TabIndex = 4;
			this.buttonBarCode.Text = "Bar codes";
			this.buttonBarCode.Click += new System.EventHandler(this.buttonBarCode_Click);
			// 
			// buttonOCR
			// 
			this.buttonOCR.Location = new System.Drawing.Point(392, 30);
			this.buttonOCR.Name = "buttonOCR";
			this.buttonOCR.Size = new System.Drawing.Size(160, 23);
			this.buttonOCR.TabIndex = 3;
			this.buttonOCR.Text = "OCR";
			this.buttonOCR.Click += new System.EventHandler(this.buttonOCR_Click);
			// 
			// buttonBrowse
			// 
			this.buttonBrowse.Location = new System.Drawing.Point(304, 30);
			this.buttonBrowse.Name = "buttonBrowse";
			this.buttonBrowse.TabIndex = 2;
			this.buttonBrowse.Text = "Browse ...";
			this.buttonBrowse.Click += new System.EventHandler(this.buttonBrowse_Click);
			// 
			// textImage
			// 
			this.textImage.Location = new System.Drawing.Point(72, 30);
			this.textImage.Name = "textImage";
			this.textImage.Size = new System.Drawing.Size(224, 22);
			this.textImage.TabIndex = 1;
			this.textImage.Text = "";
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(16, 32);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(46, 18);
			this.label3.TabIndex = 0;
			this.label3.Text = "Image:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label8
			// 
			this.label8.AutoSize = true;
			this.label8.Location = new System.Drawing.Point(8, 224);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(53, 18);
			this.label8.TabIndex = 7;
			this.label8.Text = "Results:";
			this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// textResults
			// 
			this.textResults.Location = new System.Drawing.Point(8, 248);
			this.textResults.Multiline = true;
			this.textResults.Name = "textResults";
			this.textResults.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.textResults.Size = new System.Drawing.Size(656, 184);
			this.textResults.TabIndex = 8;
			this.textResults.Text = "";
			// 
			// linkLabel1
			// 
			this.linkLabel1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.linkLabel1.Location = new System.Drawing.Point(0, 440);
			this.linkLabel1.Name = "linkLabel1";
			this.linkLabel1.Size = new System.Drawing.Size(672, 24);
			this.linkLabel1.TabIndex = 9;
			this.linkLabel1.TabStop = true;
			this.linkLabel1.Text = "Thank you for your interest in Asprise OCR. Click here to visith http://asprise.c" +
				"om/product/ocr for more details.";
			this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
			this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
			// 
			// label9
			// 
			this.label9.AutoSize = true;
			this.label9.Location = new System.Drawing.Point(16, 34);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(46, 18);
			this.label9.TabIndex = 0;
			this.label9.Text = "Image:";
			this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(672, 461);
			this.Controls.Add(this.linkLabel1);
			this.Controls.Add(this.textResults);
			this.Controls.Add(this.label8);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			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 buttonBrowse_Click(object sender, EventArgs e)
		{
			DialogResult dr = openFileDialog1.ShowDialog(this);
			textImage.Text = openFileDialog1.FileName;
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			if (checkBox1.Checked)
			{
				buttonOCRPart.Enabled = true;
			}
			else
			{
				buttonOCRPart.Enabled = false;
			}
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			buttonOCRPart.Enabled = false;
		}

		private void buttonOCR_Click(object sender, EventArgs e)
		{
			string result = OCR(textImage.Text, -1);
			textResults.Text = result;
		}

		private void buttonBarCode_Click(object sender, EventArgs e)
		{
			string result = OCRBarCodes(textImage.Text, -1);
			textResults.Text = result;
		}

		private void buttonOCRPart_Click(object sender, EventArgs e)
		{
			int startX = 0;
			int startY = 0;
			int width = -1;
			int height = -1;
			try
			{
				startX = Convert.ToInt32(textStartX.Text);
				startY = Convert.ToInt32(textStartY.Text);
				width = Convert.ToInt32(textWidth.Text);
				height = Convert.ToInt32(textHeight.Text);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.StackTrace);
			}
			String result = OCRpart(textImage.Text, -1, startX, startY, width, height);
			textResults.Text = result;
		}

		private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
				System.Diagnostics.Process.Start("http://asprise.com/product/ocr");
		}

	}
}
