using System; using System.ComponentModel; using System.Windows.Forms; using System.Drawing; using System.IO; using System.Resources; /* ImageZoom accepts an image dragged onto ImageZoom.exe icon. Window resizing causes image to fill entire Form panel. Accepts remote file Drag/Drop into Form control. Clicking image causes image to toggle between original size and full Form size. M. Gallant 03/14/2002 */ public class ImageZoom : Form { private PictureBox pictureBox1; private string filepath = @".\\zoomimage.gif"; //default file private string caption = "ImageZoom M. Gallant 03/14/2002" ; private string [] cmdargs; private Image image1; private bool originalSize = true ; private bool validimage = false; private string [] fdata ; private ResourceManager rm; public ImageZoom() : base() { this.ClientSize = new Size(200,200) ; this.Text = this.caption ; this.AllowDrop = true; // enable Drag/Drop for this form. pictureBox1 = new PictureBox() ; pictureBox1.Location = new Point(0,0) ; pictureBox1.ClientSize = this.ClientSize; //set PictureBox size to starter size of Form Controls.Add(pictureBox1); // To wire the event, create // a delegate instance and add it to the Click event. this.Click += new EventHandler(this.Form_Clicked); // form-clicked this.Resize += new EventHandler(this.Winresized); // for form itself pictureBox1.Click += new EventHandler(this.Form_Clicked) ; // --------- Define Drag/Drop event handlers for this form control --------------- this.DragEnter += new DragEventHandler(this.form_DragEnter) ; this.DragDrop += new DragEventHandler(this.form_DragHandle) ; cmdargs = Environment.GetCommandLineArgs(); if (cmdargs.Length >1) { // Console.WriteLine("CommandLine[1]: " + cmdargs[1]) ; filepath = cmdargs[1] ; if(File.Exists(filepath)) { try { image1 = Image.FromFile(filepath) ; validimage = true; pictureBox1.Image = image1 ; // display the image } catch(OutOfMemoryException ome) { validimage = false ; // invalid image file MessageBox.Show("ImageZoom: filepath: " + filepath + " is not a valid image file.", caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } // end if ... else { validimage=false; //MessageBox.Show("ImageZoom: filepath: " + filepath + " not found.", caption, //MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } // end if(cmdargs.. else { // if no image passed as argument, or dragged, use built-in default image resource rm = new ResourceManager("MigImages", this.GetType().Assembly); image1 = (Image) rm.GetObject("guitar"); pictureBox1.Image = image1 ; // display the image validimage=true; } } //end constructor public bool validImage() { return validimage ; } // Event Handler for window resizing private void Winresized(object sender, EventArgs e) { // Stretches the image to fit the pictureBox after image is originally displayed actual size pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage ; // pictureBox1.ClientSize = new Size(this.ClientRectangle.Width, this.ClientRectangle.Height) ; pictureBox1.ClientSize = this.ClientSize; originalSize = false; } // The event handler for Form click private void Form_Clicked(object sender, EventArgs e) { if (originalSize) { pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage ; } else { pictureBox1.SizeMode = PictureBoxSizeMode.Normal ; } originalSize = !originalSize ; //toggle } private void form_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) // only accept external file drag/drops e.Effect = DragDropEffects.Copy; else e.Effect = DragDropEffects.None; } private void form_DragHandle(object sender, DragEventArgs e) { fdata = (string []) e.Data.GetData(DataFormats.FileDrop) ; // cast to string array if ( verifyImage(fdata[0])) // only update if a valid image file pictureBox1.Image = Image.FromFile (fdata[0]); } private bool verifyImage(String filepath) { //don't handle dragged directories or non-image files. if(Directory.Exists(filepath)) { MessageBox.Show("ImageZoom: filepath: " + filepath + " is a directory.", caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } if(!File.Exists(filepath)) { MessageBox.Show("ImageZoom: filepath: " + filepath + " not found.", caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } try { Image image1 = Image.FromFile(filepath) ; return true; } catch(OutOfMemoryException ome) { MessageBox.Show("ImageZoom: filepath: " + filepath + " is not a valid image file.", caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } } // STAThreadAttribute indicates that Windows Forms uses the // single-threaded apartment model. [STAThreadAttribute] public static void Main(String[] args) { ImageZoom zim = new ImageZoom() ; if (!zim.validImage()) MessageBox.Show("ImageZoom: Drag & Drop any image file into the \"ImageZoom\" Form", zim.caption, MessageBoxButtons.OK, MessageBoxIcon.Information); // zim.ShowDialog() ; //modal dialog to show Form Application.Run(zim); } }