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

namespace Canvas_Image
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private C1.C1Flash.C1FlashCanvas c1FlashCanvas1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.RadioButton rbBestFit;
		private System.Windows.Forms.RadioButton rbActualSize;
		/// <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()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.c1FlashCanvas1 = new C1.C1Flash.C1FlashCanvas();
			this.button1 = new System.Windows.Forms.Button();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.rbBestFit = new System.Windows.Forms.RadioButton();
			this.rbActualSize = new System.Windows.Forms.RadioButton();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(384, 32);
			this.label1.TabIndex = 0;
			this.label1.Text = "This sample demonstrates how to render images using the C1FlashCanvas class.";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(128, 184);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(168, 56);
			this.button1.TabIndex = 3;
			this.button1.Text = "Browse and view a image file on my machine.";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(400, 40);
			this.label2.TabIndex = 6;
			this.label2.Text = "You can draw any image which is supported by the .NET framework onto the C1FlashC" +
				"anvas, since the Flash only supports JPEG and BMP, C1Flash does the nessasary co" +
				"nvertion if not supported.";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 96);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(384, 16);
			this.label3.TabIndex = 7;
			this.label3.Text = "For Example: JPEG, BMP, GIF, TIFF, PNG, ICON and WMF....";
			// 
			// rbBestFit
			// 
			this.rbBestFit.Checked = true;
			this.rbBestFit.Location = new System.Drawing.Point(136, 152);
			this.rbBestFit.Name = "rbBestFit";
			this.rbBestFit.Size = new System.Drawing.Size(64, 24);
			this.rbBestFit.TabIndex = 8;
			this.rbBestFit.TabStop = true;
			this.rbBestFit.Text = "Best fit";
			// 
			// rbActualSize
			// 
			this.rbActualSize.Location = new System.Drawing.Point(216, 152);
			this.rbActualSize.Name = "rbActualSize";
			this.rbActualSize.Size = new System.Drawing.Size(88, 24);
			this.rbActualSize.TabIndex = 9;
			this.rbActualSize.Text = "Actual Size";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(432, 270);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.rbActualSize,
																		  this.rbBestFit,
																		  this.label3,
																		  this.label2,
																		  this.button1,
																		  this.label1});
			this.Name = "Form1";
			this.Text = "ComponentOne C1Flash Sample - Images";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void LaunchViewer(string filename)
		{
			try
			{
				System.Diagnostics.Process.Start(filename);
			}
			catch (Exception e)
			{
				System.Diagnostics.Process.Start("IEXPLORE.EXE", filename);
			}
		}

		private string tempdir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

		private void button1_Click(object sender, System.EventArgs e)
		{
			this.openFileDialog1.InitialDirectory = Application.StartupPath;
			this.openFileDialog1.Filter = "All Image Files|*.png;*.emf;*wmf;*tif;*tiff;*.gif;*.jpg;*.jpe;*.jpeg;*.bmp;*.dib;*.rle|BMP(*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|JPEG(*.jpg;*.jpe;*.jpeg)|*.jpg;*.jpe;*.jpeg|WMF(*.wmf;*.emf)|*.wmf;*.emf|TIFF(*.tif;*tiff)|*.tif;*tiff|GIF(*.gif)|*.gif|PNG(*.png)|*.png|ICON(*.ico)|*.ico";
			if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				string filename = this.openFileDialog1.FileName.Trim();
				if (filename != null && filename.Length > 0)
				{
					Image image = Image.FromFile(filename);
					this.c1FlashCanvas1.Clear(Color.White);

					float width = image.Width;
					float height = image.Height;
					if (this.rbBestFit.Checked)
					{
						float ratio = (float)image.Width/(float)image.Height;
						if (image.Width > image.Height)
						{
							width = this.c1FlashCanvas1.Width;
							height = (int)(width/ratio);

							if (height > this.c1FlashCanvas1.Height)
							{
								height = this.c1FlashCanvas1.Height;
								width = height * ratio;
							}
						}
						else
						{
							height = this.c1FlashCanvas1.Height;
							width = (int)(height * ratio);

							if (width > this.c1FlashCanvas1.Width)
							{
								width = this.c1FlashCanvas1.Width;
								height = width / ratio;
							}
						}
					}

					float x = (this.c1FlashCanvas1.Width - width)/2;
					float y = (this.c1FlashCanvas1.Height - height)/2;

					this.c1FlashCanvas1.DrawImage(image, new RectangleF(x, y, width, height));
					image.Dispose();

					this.c1FlashCanvas1.RenderToFile(tempdir + @"\c1flash_canvas_image.swf");
					LaunchViewer(tempdir + @"\c1flash_canvas_image.swf");
				}
			}
		}
	}
}
