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
	{
		public System.Windows.Forms.TextBox Text1;
		public System.Windows.Forms.Button Command3;
		public System.Windows.Forms.Button Command2;
		public System.Windows.Forms.Button Command1;
		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.Text1 = new System.Windows.Forms.TextBox();
			this.Command3 = new System.Windows.Forms.Button();
			this.Command2 = new System.Windows.Forms.Button();
			this.Command1 = new System.Windows.Forms.Button();
			this.ImageKit1 = new AxIMAGEKIT7Lib.AxImageKit();
			((System.ComponentModel.ISupportInitialize)(this.ImageKit1)).BeginInit();
			this.SuspendLayout();
			// 
			// 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)(64)), ((System.Byte)(0)));
			this.Text1.Location = new System.Drawing.Point(6, 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(537, 64);
			this.Text1.TabIndex = 8;
			this.Text1.Text = @"Many people ask how to print an image in ""actual size"". Because digital images are made up of pixels, whose size depends on the resolution of the output device, there is no actual size. Fortunately, digital images have a resolution property which, if set, tells the resolution of the input device at the time the image was created. This information can be used to output an image at the same size as the original when it was captured.";
			// 
			// Command3
			// 
			this.Command3.BackColor = System.Drawing.SystemColors.Control;
			this.Command3.Cursor = System.Windows.Forms.Cursors.Default;
			this.Command3.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Command3.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Command3.Location = new System.Drawing.Point(462, 398);
			this.Command3.Name = "Command3";
			this.Command3.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Command3.Size = new System.Drawing.Size(81, 33);
			this.Command3.TabIndex = 7;
			this.Command3.Text = "End";
			this.Command3.Click += new System.EventHandler(this.Command3_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(462, 150);
			this.Command2.Name = "Command2";
			this.Command2.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Command2.Size = new System.Drawing.Size(81, 33);
			this.Command2.TabIndex = 6;
			this.Command2.Text = "Print";
			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(462, 86);
			this.Command1.Name = "Command1";
			this.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Command1.Size = new System.Drawing.Size(81, 33);
			this.Command1.TabIndex = 5;
			this.Command1.Text = "Load";
			this.Command1.Click += new System.EventHandler(this.Command1_Click);
			// 
			// ImageKit1
			// 
			this.ImageKit1.Location = new System.Drawing.Point(8, 72);
			this.ImageKit1.Name = "ImageKit1";
			this.ImageKit1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("ImageKit1.OcxState")));
			this.ImageKit1.Size = new System.Drawing.Size(449, 361);
			this.ImageKit1.TabIndex = 9;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(548, 436);
			this.Controls.Add(this.ImageKit1);
			this.Controls.Add(this.Text1);
			this.Controls.Add(this.Command3);
			this.Controls.Add(this.Command2);
			this.Controls.Add(this.Command1);
			this.Name = "Form1";
			this.Text = "Printing In Actual Size";
			((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)
		{
			//displays the 'Open File' dialog and allows you to select an image to load...
			if (ImageKit1.File.OpenFileDialog() == false)
			{
				return;
			}
			
			//loads the image...
			ImageKit1.File.LoadFile(IMAGEKIT7Lib.LoadFileConstants.ikLoad);
			
			//displays the image in scale display mode...
			ImageKit1.Display(IMAGEKIT7Lib.DispModeConstants.ikScale);
		}

		private void Command2_Click(object sender, System.EventArgs e)
		{
			bool ret;
			int ActualHeight;
			int ActualWidth;
			int intLeft;
			int intTop;
			int intRight;
			int intBottom;

			//Get the image's resolution...
			ImageKit1.GetDpi();

			if (ImageKit1.Xdpi == 0)
			{
				MessageBox.Show("This image does not have a value set in its resolution property");
				return;
			}
			if (ImageKit1.Ydpi == 0) 
			{
				MessageBox.Show("This image does not have a value set in its resolution property");
				return;
			}

			//Get the image's information...
			ImageKit1.GetImageType();

			//Calculate the "actual size" of the image in inches...
			ActualHeight = ImageKit1.ImageHeight / ImageKit1.Xdpi;
			ActualWidth = ImageKit1.ImageWidth / ImageKit1.Ydpi;

			//convert this to 0.1 millimeters which is the unit size
			//that the print control uses....
			ActualHeight = ActualHeight * 254;
			ActualWidth = ActualWidth * 254;

			//calculate the left, top, right and bottom parameters
			//for the ImageOut method. I want the image to be centered in
			// the printed page and have a maximum size of
			//left=100, top=100, right=1950, bottom=2600
			if (ActualHeight > 2500) 
			{
				intTop = 100;
				intBottom = 2600;
			}
			else 
			{
				intTop = 1250 - (ActualHeight / 2);
				intBottom = 1250 + (ActualHeight / 2);
			}

			if (ActualWidth > 1850) 
			{
				intLeft = 100;
				intRight = 1950;
			}
			else 
			{
				intLeft = 925 - (ActualWidth / 2);
				intRight = 925 + (ActualWidth / 2);
			}

			//display the "Print" dialog...
			ret = ImageKit1.PrintDraw.PrintDialog();

			// Print the displayed image in the center of the paper (portrait orientation)
			ret = ImageKit1.PrintDraw.PrintStartDoc();
			ret = ImageKit1.PrintDraw.PrintStartPage();
			if (ImageKit1.ImageHandle != 0) 
			{
				ret = ImageKit1.PrintDraw.ImageOut(ImageKit1.PrintDraw.hDC, ImageKit1.ImageHandle, intLeft, intTop, intRight, intBottom, true, false,IMAGEKIT7Lib.OutPutDeviceModeConstants.ikPrinter);
			}
			ImageKit1.PrintDraw.PrintEndPage();
			ImageKit1.PrintDraw.PrintEndDoc();
		}

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