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

namespace Project1
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		public System.Windows.Forms.TextBox Text2;
		public System.Windows.Forms.TextBox Text1;
		public System.Windows.Forms.Button Command2;
		public System.Windows.Forms.Button Command1;
		public System.Windows.Forms.Label Label1;
		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.Text2 = new System.Windows.Forms.TextBox();
			this.Text1 = new System.Windows.Forms.TextBox();
			this.Command2 = new System.Windows.Forms.Button();
			this.Command1 = new System.Windows.Forms.Button();
			this.Label1 = new System.Windows.Forms.Label();
			this.ImageKit1 = new AxIMAGEKIT7Lib.AxImageKit();
			((System.ComponentModel.ISupportInitialize)(this.ImageKit1)).BeginInit();
			this.SuspendLayout();
			// 
			// Text2
			// 
			this.Text2.AcceptsReturn = true;
			this.Text2.AutoSize = false;
			this.Text2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
			this.Text2.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.Text2.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Text2.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(0)), ((System.Byte)(0)));
			this.Text2.Location = new System.Drawing.Point(9, 0);
			this.Text2.MaxLength = 0;
			this.Text2.Multiline = true;
			this.Text2.Name = "Text2";
			this.Text2.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Text2.Size = new System.Drawing.Size(497, 49);
			this.Text2.TabIndex = 10;
			this.Text2.Text = "This sample shows how to save images in a database. A sample MDB database is incl" +
				"uded. Set an unique id for the image, then click the button to load and save it " +
				"in the database. Note: if the Image ID is not unique then an error will occur.";
			// 
			// 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.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Text1.ForeColor = System.Drawing.SystemColors.WindowText;
			this.Text1.Location = new System.Drawing.Point(457, 64);
			this.Text1.MaxLength = 0;
			this.Text1.Name = "Text1";
			this.Text1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Text1.Size = new System.Drawing.Size(41, 18);
			this.Text1.TabIndex = 8;
			this.Text1.Text = "001";
			// 
			// 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(385, 304);
			this.Command2.Name = "Command2";
			this.Command2.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Command2.Size = new System.Drawing.Size(121, 41);
			this.Command2.TabIndex = 7;
			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.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(385, 96);
			this.Command1.Name = "Command1";
			this.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Command1.Size = new System.Drawing.Size(121, 41);
			this.Command1.TabIndex = 6;
			this.Command1.Text = "Load image and save to database";
			this.Command1.Click += new System.EventHandler(this.Command1_Click);
			// 
			// Label1
			// 
			this.Label1.BackColor = System.Drawing.SystemColors.Control;
			this.Label1.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label1.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Label1.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label1.Location = new System.Drawing.Point(393, 64);
			this.Label1.Name = "Label1";
			this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label1.Size = new System.Drawing.Size(57, 17);
			this.Label1.TabIndex = 9;
			this.Label1.Text = "Image ID";
			this.Label1.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// ImageKit1
			// 
			this.ImageKit1.Location = new System.Drawing.Point(8, 56);
			this.ImageKit1.Name = "ImageKit1";
			this.ImageKit1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("ImageKit1.OcxState")));
			this.ImageKit1.Size = new System.Drawing.Size(369, 304);
			this.ImageKit1.TabIndex = 11;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(514, 371);
			this.Controls.Add(this.ImageKit1);
			this.Controls.Add(this.Text2);
			this.Controls.Add(this.Text1);
			this.Controls.Add(this.Command2);
			this.Controls.Add(this.Command1);
			this.Controls.Add(this.Label1);
			this.Name = "Form1";
			this.Text = "Saving Images To Database";
			((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;
		
			if (ImageKit1.File.OpenFileDialog() == false) 
			{
				return;
			}
		
			// load the image
			ImageKit1.File.LoadFile(IMAGEKIT7Lib.LoadFileConstants.ikLoad);
		
			//here we save the image data to the memory.
			//it is saved as a JPEG image, the "0" parameter means
			//the image is a base image layer
			ret = ImageKit1.File.SaveFileMem(IMAGEKIT7Lib.SaveFileConstants.ikSaveJPEG, 0);
			if (ret == false) 
			{
				MessageBox.Show("Unable to save image to stream");
				return;
			}
			
			//stream the image data into a byte array
			byte[] fileData = (byte[])ImageKit1.File.SaveToStream();

			//connect to the database
			string connString ="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Application.StartupPath + "\\..\\..\\ImageDataBlob.mdb;";
			OleDbConnection oleDbConnection1 = new OleDbConnection(connString);

			string cmdText ="INSERT INTO FILES(ID," + " ImageData) values(@ID,@ImageData)";
			OleDbCommand oleDbCommand1 = new OleDbCommand(cmdText,oleDbConnection1);

			oleDbConnection1.Open();
			if (oleDbCommand1.Parameters.Count ==0 )
			{
				oleDbCommand1.Parameters.Add("@ID", System.Data.OleDb.OleDbType.Char,50);
				oleDbCommand1.Parameters.Add("@ImageData", System.Data.OleDb.OleDbType.Binary);
			}

			oleDbCommand1.Parameters["@ID"].Value=this.Text1.Text;
			oleDbCommand1.Parameters["@ImageData"].Value=fileData;

			int iresult=oleDbCommand1.ExecuteNonQuery();
			oleDbConnection1.Close();
			oleDbCommand1.Dispose();			

			ImageKit1.CtlRefresh();

			if (iresult == 1) 
			{
				MessageBox.Show("Image added to database");
			}
			else 
			{
				MessageBox.Show("Unable to add image to database");
			}
		}

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

	}
}
