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.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.TextBox textBox1;
		private AxIMAGEKIT7Lib.AxImageKit ImageKit1;
		private System.Windows.Forms.Button button3;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.RadioButton radioButton1;
		private System.Windows.Forms.RadioButton radioButton2;
		private System.Windows.Forms.RadioButton radioButton3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.GroupBox groupBox1;
		private short BitCount;
		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.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.ImageKit1 = new AxIMAGEKIT7Lib.AxImageKit();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button3 = 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.button4 = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.ImageKit1)).BeginInit();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(504, 80);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(112, 32);
			this.button1.TabIndex = 0;
			this.button1.Text = "Load Image";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(504, 432);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(112, 32);
			this.button2.TabIndex = 1;
			this.button2.Text = "End";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// ImageKit1
			// 
			this.ImageKit1.Location = new System.Drawing.Point(8, 80);
			this.ImageKit1.Name = "ImageKit1";
			this.ImageKit1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("ImageKit1.OcxState")));
			this.ImageKit1.Size = new System.Drawing.Size(488, 400);
			this.ImageKit1.TabIndex = 2;
			// 
			// 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(616, 72);
			this.textBox1.TabIndex = 3;
			this.textBox1.Text = @"This sample illustrates how to use the ConvertColor method to change the bitcount of an image. Because different image formats support different bitcounts, it may be necessary to convert the bitcount before an image can be saved. Common examples of this are JPEG images which can only be 24 bit color or 8 bit grayscale and GIF images which must be 8 bits or less. The ImageKit supports 1, 4, 8, 16, 24, and 32 bit color as well as 4 and 8 bit grayscale. One thing to keep in mind when using the ConvertColor method is that it will fail if you try to convert an image to the same bitcount that it was before conversion.";
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(504, 240);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(112, 32);
			this.button3.TabIndex = 6;
			this.button3.Text = "Convert Color";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.radioButton3);
			this.groupBox1.Controls.Add(this.radioButton2);
			this.groupBox1.Controls.Add(this.radioButton1);
			this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.groupBox1.Location = new System.Drawing.Point(504, 136);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(120, 88);
			this.groupBox1.TabIndex = 7;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Convert To...";
			// 
			// radioButton1
			// 
			this.radioButton1.Location = new System.Drawing.Point(8, 24);
			this.radioButton1.Name = "radioButton1";
			this.radioButton1.Size = new System.Drawing.Size(96, 16);
			this.radioButton1.TabIndex = 0;
			this.radioButton1.Text = "1 Bit Color";
			this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
			// 
			// radioButton2
			// 
			this.radioButton2.Location = new System.Drawing.Point(8, 40);
			this.radioButton2.Name = "radioButton2";
			this.radioButton2.Size = new System.Drawing.Size(104, 16);
			this.radioButton2.TabIndex = 1;
			this.radioButton2.Text = "8 Bit Grayscale";
			this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
			// 
			// radioButton3
			// 
			this.radioButton3.Location = new System.Drawing.Point(8, 56);
			this.radioButton3.Name = "radioButton3";
			this.radioButton3.Size = new System.Drawing.Size(96, 16);
			this.radioButton3.TabIndex = 2;
			this.radioButton3.Text = "24 Bit Color";
			this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(504, 352);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(112, 32);
			this.button4.TabIndex = 8;
			this.button4.Text = "Save As Bmp";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(632, 486);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.ImageKit1);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.button3);
			this.Name = "Form1";
			this.Text = "Getting Started With Transformations - Converting Bitcount";
			((System.ComponentModel.ISupportInitialize)(this.ImageKit1)).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 button2_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		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 in actual size mode...
			ImageKit1.ScrollBar=true;
			ImageKit1.Display(IMAGEKIT7Lib.DispModeConstants.ikActualSize);

			//show the image bitcount and whether it is grayscale
			if (ImageKit1.Gray == false)
			{
				MessageBox.Show("This image is " + ImageKit1.BitCount + " bit color");
			}
			else
			{
				MessageBox.Show("This image is " + ImageKit1.BitCount + " bit grayscale");
			}
			
		}



		private void button3_Click(object sender, System.EventArgs e)
		{
			bool ret;
			if (BitCount == 1) 
			{
				//check to make sure the image isn't 1 bit color...
				if (ImageKit1.BitCount == 1) 
				{
					MessageBox.Show("This image is already 1 bit color");
					return;
				}
				//execute ConvertColor method
				ret = ImageKit1.Effect.ConvertColor(1, false, false, 128);
			}
			else if (BitCount == 80) 
			{
				//check to make sure the image isn't 8 bit grayscale...
				if (ImageKit1.BitCount == 8) 
				{
					if (ImageKit1.Gray == true) 
					{
						MessageBox.Show("This image is already 8 bit grayscale");
						return;
					}
				}
				//execute ConvertColor method
				ret = ImageKit1.Effect.ConvertColor(80, false, false, 128);
			}
			else if (BitCount == 24) 
			{
				//check to make sure the image isn't 24 bit color...
				if (ImageKit1.BitCount == 24) 
				{
					MessageBox.Show("This image is already 24 bit color");
					return;
				}
				//execute ConvertColor method
				ret = ImageKit1.Effect.ConvertColor(24, false, false, 128);
			}
			else
			{
				MessageBox.Show("Please choose a bit count");
			}
			ImageKit1.CtlRefresh();

		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			bool ret;
			//save the image as a bmp image...
			ImageKit1.File.FileName = Application.StartupPath + "\\test.bmp";
			ret = ImageKit1.File.SaveFile(IMAGEKIT7Lib.SaveFileConstants.ikSaveBMP, 0);
		
			if (ret == true) 
			{
				MessageBox.Show("Image saved in the following directory: " + ImageKit1.File.FileName);
			}
			else 
			{
				MessageBox.Show("Unable to save image");
			}

		}

		private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
		{
			BitCount = 1;
		}

		private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
		{
			BitCount = 80;
		}

		private void radioButton3_CheckedChanged(object sender, System.EventArgs e)
		{
			BitCount = 24;
		}


	}
}
