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

namespace Contrast
{
	/// <summary>
	/// 
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		public System.Windows.Forms.TextBox Text1;
		public System.Windows.Forms.HScrollBar HScroll1;
		public System.Windows.Forms.Button Command2;
		public System.Windows.Forms.Button Command1;
		public System.Windows.Forms.Label Label6;
		public System.Windows.Forms.Label Label5;
		public System.Windows.Forms.Label Label4;
		private AxIMAGEKIT7Lib.AxImageKit axImageKit1;
		/// <summary>
		///
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.TextBox textBox1;

		private int ImgHandle;

		public Form1()
		{
			//
			//
			//
			InitializeComponent();

			//
			// 
			//
		}

		/// <summary>
		/// 
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region ""
		/// <summary>
		
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.Text1 = new System.Windows.Forms.TextBox();
			this.HScroll1 = new System.Windows.Forms.HScrollBar();
			this.Command2 = new System.Windows.Forms.Button();
			this.Command1 = new System.Windows.Forms.Button();
			this.Label6 = new System.Windows.Forms.Label();
			this.Label5 = new System.Windows.Forms.Label();
			this.Label4 = new System.Windows.Forms.Label();
			this.axImageKit1 = new AxIMAGEKIT7Lib.AxImageKit();
			this.textBox1 = new System.Windows.Forms.TextBox();
			((System.ComponentModel.ISupportInitialize)(this.axImageKit1)).BeginInit();
			this.SuspendLayout();
			// 
			// Text1
			// 
			this.Text1.AcceptsReturn = true;
			this.Text1.AutoSize = false;
			this.Text1.BackColor = System.Drawing.SystemColors.Window;
			this.Text1.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.Text1.ForeColor = System.Drawing.SystemColors.WindowText;
			this.Text1.Location = new System.Drawing.Point(368, 472);
			this.Text1.MaxLength = 0;
			this.Text1.Name = "Text1";
			this.Text1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Text1.Size = new System.Drawing.Size(41, 20);
			this.Text1.TabIndex = 10;
			this.Text1.Text = "0";
			this.Text1.TextChanged += new System.EventHandler(this.Text1_TextChanged);
			// 
			// HScroll1
			// 
			this.HScroll1.Cursor = System.Windows.Forms.Cursors.Default;
			this.HScroll1.LargeChange = 5;
			this.HScroll1.Location = new System.Drawing.Point(128, 472);
			this.HScroll1.Maximum = 14;
			this.HScroll1.Minimum = -10;
			this.HScroll1.Name = "HScroll1";
			this.HScroll1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.HScroll1.Size = new System.Drawing.Size(233, 18);
			this.HScroll1.TabIndex = 9;
			this.HScroll1.TabStop = true;
			this.HScroll1.ValueChanged += new System.EventHandler(this.HScroll1_ValueChanged);
			this.HScroll1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.HScroll1_Scroll);
			// 
			// Command2
			// 
			this.Command2.BackColor = System.Drawing.SystemColors.Control;
			this.Command2.Cursor = System.Windows.Forms.Cursors.Default;
			this.Command2.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Command2.Location = new System.Drawing.Point(408, 412);
			this.Command2.Name = "Command2";
			this.Command2.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Command2.Size = new System.Drawing.Size(87, 47);
			this.Command2.TabIndex = 8;
			this.Command2.Text = "End";
			this.Command2.Click += new System.EventHandler(this.Command2_Click);
			// 
			// Command1
			// 
			this.Command1.BackColor = System.Drawing.SystemColors.Control;
			this.Command1.Cursor = System.Windows.Forms.Cursors.Default;
			this.Command1.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Command1.Location = new System.Drawing.Point(20, 408);
			this.Command1.Name = "Command1";
			this.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Command1.Size = new System.Drawing.Size(87, 47);
			this.Command1.TabIndex = 7;
			this.Command1.Text = "Load Image";
			this.Command1.Click += new System.EventHandler(this.Command1_Click);
			// 
			// Label6
			// 
			this.Label6.BackColor = System.Drawing.SystemColors.Control;
			this.Label6.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label6.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Label6.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label6.Location = new System.Drawing.Point(128, 448);
			this.Label6.Name = "Label6";
			this.Label6.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label6.Size = new System.Drawing.Size(97, 19);
			this.Label6.TabIndex = 13;
			this.Label6.Text = "<-- Decrease";
			// 
			// Label5
			// 
			this.Label5.BackColor = System.Drawing.SystemColors.Control;
			this.Label5.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label5.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Label5.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label5.Location = new System.Drawing.Point(272, 448);
			this.Label5.Name = "Label5";
			this.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label5.Size = new System.Drawing.Size(97, 19);
			this.Label5.TabIndex = 12;
			this.Label5.Text = "Increase -->";
			// 
			// Label4
			// 
			this.Label4.BackColor = System.Drawing.SystemColors.Control;
			this.Label4.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label4.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Label4.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label4.Location = new System.Drawing.Point(172, 408);
			this.Label4.Name = "Label4";
			this.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label4.Size = new System.Drawing.Size(169, 27);
			this.Label4.TabIndex = 11;
			this.Label4.Text = "Change Contrast";
			this.Label4.TextAlign = System.Drawing.ContentAlignment.TopCenter;
			// 
			// axImageKit1
			// 
			this.axImageKit1.Location = new System.Drawing.Point(12, 56);
			this.axImageKit1.Name = "axImageKit1";
			this.axImageKit1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axImageKit1.OcxState")));
			this.axImageKit1.Size = new System.Drawing.Size(492, 344);
			this.axImageKit1.TabIndex = 14;
			// 
			// 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(4, 0);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(508, 48);
			this.textBox1.TabIndex = 15;
			this.textBox1.Text = "The RGBSpline method can be used to manipulate RGB values in a variety of ways. I" +
				"n this sample, we use the RGBSpline method to adjust the contrast in an image. F" +
				"or more details about the RGBSpline method, please refer to the Help Documentati" +
				"on.";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(520, 504);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.axImageKit1);
			this.Controls.Add(this.Text1);
			this.Controls.Add(this.HScroll1);
			this.Controls.Add(this.Command2);
			this.Controls.Add(this.Command1);
			this.Controls.Add(this.Label6);
			this.Controls.Add(this.Label5);
			this.Controls.Add(this.Label4);
			this.Name = "Form1";
			this.Text = "Manipulating Color - Contrast";
			((System.ComponentModel.ISupportInitialize)(this.axImageKit1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Command1_Click(object sender, System.EventArgs e)
		{
			// Displays the Open File dialog to select an image...
			if (!axImageKit1.File.OpenFileDialog())
				return;

			// Loads the image...
			axImageKit1.File.LoadFile(IMAGEKIT7Lib.LoadFileConstants.ikLoad);

			//display the image is scale display mode...
			axImageKit1.Display(IMAGEKIT7Lib.DispModeConstants.ikScale);

			ImgHandle = axImageKit1.CopyImage(axImageKit1.ImageHandle);
		}

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

		private void Text1_TextChanged(object sender, System.EventArgs e)
		{
			try{
		        HScroll1.Value = Convert.ToInt32(Text1.Text);
			}
			catch{
			}
		}

		private void RgbSpline(int val)
		{
			bool ret = false;
			int []x = new int[5];
			int []y = new int[5];
			int []dummy_x = new int[1];
			int []dummy_y = new int[1];

			
			// Make a copy of the original image and apply effect to the copy.
			axImageKit1.ImageHandle = axImageKit1.CopyImage(ImgHandle);

			//apply effect to the total image...
			axImageKit1.Effect.SelectMode = IMAGEKIT7Lib.AreaSelectModeConstants.ikEffectAll;

			//the x array holds input rgb values
			//the y array holds output rgb values
			//the spline method interpolates these 5 points
			// and outputs new rgb values
			//when HScroll1.Value is positive there is an increase in contrast
			//when HScroll1.Value is negative there is an decrease in contrast
			//please refer to ImageKit7 Help file Effect Control Spline method.
			x[0] = 0;
			y[0] = 0 - (val * 12);
			x[1] = 64;
			y[1] = 64 - (val * 6);
			x[2] = 128;
			y[2] = 128;
			x[3] = 192;
			y[3] = 192 + (val * 6);
			x[4] = 255;
			y[4] = 255 + (val * 12);

			ret = axImageKit1.Effect.RGBSpline (ref dummy_x[0], ref dummy_y[0], 0, ref x[0], ref y[0], 5, true, true, true);

			//display the image is scale display mode...
			axImageKit1.Display(IMAGEKIT7Lib.DispModeConstants.ikScale);

			Text1.Text = val.ToString();
		}

		private void HScroll1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
		{
			RgbSpline(e.NewValue);
		}

		private void HScroll1_ValueChanged(object sender, System.EventArgs e)
		{
			RgbSpline(HScroll1.Value);
		}
	}
}
