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

namespace Project1
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private AxIMAGEKIT7Lib.AxImageKit ImageKit1;
		public System.Windows.Forms.TextBox Text1;
		public System.Windows.Forms.Button Command1;
		public System.Windows.Forms.Button Command2;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton radioButton1;
		private System.Windows.Forms.RadioButton radioButton2;
		private System.Windows.Forms.RadioButton radioButton3;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private string ImageID;

		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.ImageKit1 = new AxIMAGEKIT7Lib.AxImageKit();
			this.Text1 = new System.Windows.Forms.TextBox();
			this.Command1 = new System.Windows.Forms.Button();
			this.Command2 = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.radioButton3 = new System.Windows.Forms.RadioButton();
			this.radioButton2 = new System.Windows.Forms.RadioButton();
			this.radioButton1 = new System.Windows.Forms.RadioButton();
			((System.ComponentModel.ISupportInitialize)(this.ImageKit1)).BeginInit();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// 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, 273);
			this.ImageKit1.TabIndex = 0;
			// 
			// 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(488, 49);
			this.Text1.TabIndex = 7;
			this.Text1.Text = "BLOB image data can be streamed from the database to the ImageKit. It is then con" +
				"verted to DIB data and displayed in the ImageKit Control. This sample has a smal" +
				"l MDB database included which contains three images.\r\n";
			// 
			// 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(384, 160);
			this.Command1.Name = "Command1";
			this.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Command1.Size = new System.Drawing.Size(112, 41);
			this.Command1.TabIndex = 8;
			this.Command1.Text = "Retrieve image from database";
			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(384, 280);
			this.Command2.Name = "Command2";
			this.Command2.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Command2.Size = new System.Drawing.Size(112, 41);
			this.Command2.TabIndex = 9;
			this.Command2.Text = "End";
			this.Command2.Click += new System.EventHandler(this.Command2_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.radioButton3);
			this.groupBox1.Controls.Add(this.radioButton2);
			this.groupBox1.Controls.Add(this.radioButton1);
			this.groupBox1.Location = new System.Drawing.Point(384, 64);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(112, 80);
			this.groupBox1.TabIndex = 10;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Database Images";
			// 
			// radioButton3
			// 
			this.radioButton3.Location = new System.Drawing.Point(16, 56);
			this.radioButton3.Name = "radioButton3";
			this.radioButton3.Size = new System.Drawing.Size(80, 16);
			this.radioButton3.TabIndex = 2;
			this.radioButton3.Text = "Image 3";
			this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
			// 
			// radioButton2
			// 
			this.radioButton2.Location = new System.Drawing.Point(16, 40);
			this.radioButton2.Name = "radioButton2";
			this.radioButton2.Size = new System.Drawing.Size(80, 16);
			this.radioButton2.TabIndex = 1;
			this.radioButton2.Text = "Image 2";
			this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
			// 
			// radioButton1
			// 
			this.radioButton1.Location = new System.Drawing.Point(16, 24);
			this.radioButton1.Name = "radioButton1";
			this.radioButton1.Size = new System.Drawing.Size(80, 16);
			this.radioButton1.TabIndex = 0;
			this.radioButton1.Text = "Image 1";
			this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(504, 336);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.Command2);
			this.Controls.Add(this.Command1);
			this.Controls.Add(this.Text1);
			this.Controls.Add(this.ImageKit1);
			this.Name = "Form1";
			this.Text = "Loading Images From Database";
			this.Load += new System.EventHandler(this.Form1_Load);
			((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 Command1_Click(object sender, System.EventArgs e)
		{
			bool ret;

			string connString ="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Application.StartupPath + "\\..\\..\\ImageDataBlob.mdb;";
			OleDbConnection oleDbConnection1 = new OleDbConnection(connString);
			
			//build a Select Statement...			
			string cmdStatement ="select ImageData from Files where ID='" + ImageID + "'";			
			OleDbCommand cmdSelect = new OleDbCommand(cmdStatement,oleDbConnection1);

			//open database connection...
			oleDbConnection1.Open();
			//retrieve image data in a byte array...
			byte[] img=(byte[])cmdSelect.ExecuteScalar();
			//close database connection
			oleDbConnection1.Close();
				
			//load byte array (raw image data) into the ImageKit
			ImageKit1.File.LoadFromStream(img); //Get the BLOB data from the stream
			
			// convert raw date to dib data and display...
			ret=ImageKit1.File.LoadFileMem(IMAGEKIT7Lib.LoadFileConstants.ikLoad); //Load the data as DIB into the ImageKit Control
			ImageKit1.Display(IMAGEKIT7Lib.DispModeConstants.ikScale); //Display the image

			
		}

		private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
		{
			ImageID = "001";
		}

		private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
		{
			ImageID = "002";
		}

		private void radioButton3_CheckedChanged(object sender, System.EventArgs e)
		{
			ImageID = "003";
		}

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

		private void Form1_Load(object sender, System.EventArgs e)
		{
			radioButton1.Checked=true;
		}
	}
}
