Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

Image Viewer Application - Winforms & C#
By Jim Joy .C

The following is an Image viewer application which can
be used to view image files of many formats. This example
is provides a good introduction to Winforms.

For compiling the code, use the following at the command line:

csc /target:winexe /reference:System.dll /reference:System.Winforms.dll /reference:System.Drawing.dll /reference:Microsoft.Win32.Interop.dll PictureViewer.cs

Source Code

namespace myPictViewer
{
using System;
	using System.WinForms;
	using System.Drawing;
	public class PictViewForm : Form
	{
		protected Bitmap myPicture;
		protected bool myPicStyle = true;
		protected MenuItem showWindow;
		protected MenuItem showNative;
		protected MenuItem closePicture;
		public PictViewForm()
		{
			this.Text = "Picture Viewer";
			this.ClientSize = new Size(640,480);
			MainMenu myMainMenu = new MainMenu();
			MenuItem myFileItem = myMainMenu.MenuItems.Add("&File");
			MenuItem myHelpItem = myMainMenu.MenuItems.Add("&Help");
			//The line below is commented out as it may not work for Beta 1 of .NET
			//myFileItem.Popup += new EventHandler (checkMenus);
			myFileItem.MenuItems.Add(new MenuItem("&Open" , new EventHandler(OnOpen) , Shortcut.CtrlO));
			myFileItem.MenuItems.Add("-");
			myFileItem.MenuItems.Add(showWindow = new MenuItem("Show Image - Fit To Window" , new EventHandler(ShowWindow)));
			myFileItem.MenuItems.Add(showNative = new MenuItem("Show Image - Native Size" , new EventHandler(ShowNative)));
			myFileItem.MenuItems.Add("-");
			myFileItem.MenuItems.Add(closePicture = new MenuItem("&Close Picture" , new EventHandler(OnClose) , Shortcut.CtrlH));
			myFileItem.MenuItems.Add(new MenuItem("E&xit" , new EventHandler(OnExit) , Shortcut.CtrlE));

			myHelpItem.MenuItems.Add(new MenuItem("&About" , new EventHandler(OnAbout) , Shortcut.CtrlA));
			closePicture.Enabled = false;
			this.Menu = myMainMenu;
			this.StartPosition = FormStartPosition.CenterScreen;
		}

		protected override void OnPaint(PaintEventArgs myArgs)
		{
			if (myPicture != null)		
			{
				Graphics myGraph = myArgs.Graphics;
				if (myPicStyle == true)
				{
					myGraph.DrawImage(myPicture , AutoScrollPosition.X , AutoScrollPosition.Y , myPicture.Width , myPicture.Height);
				}
				else
				{
					myGraph.DrawImage(myPicture , ClientRectangle);				
				}
			}	
		}

		private void OnAbout(Object mySender , EventArgs myArgs)
		{
			MessageBox.Show("Picture Viewer 1.0 by Jim","About Picture Viewer",MessageBox.OK|MessageBox.IconInformation);
		}

		private void checkMenus(Object mySender , EventArgs myArgs)
		{
			if (myPicture != null)
			{
				if (myPicStyle == true)
				{
					showNative.Checked = true;
					showWindow.Checked = false;
				}
				else
				{
					showNative.Checked = false;
					showWindow.Checked = true;
				}
			}
			else
			{
				showNative.Checked = false;
				showWindow.Checked = false;
				myPicStyle = true; 
			}
		}
	
		private void ShowWindow(Object mySender , EventArgs myArgs)
		{
			myPicStyle = false;
			this.SetStyle (ControlStyles.ResizeRedraw, true);
			if (myPicture != null)
			{			
				this.AutoScroll = false;
				this.Invalidate();
			}
		}

		private void ShowNative(Object mySender , EventArgs myArgs)
		{
			myPicStyle = true;
			this.SetStyle (ControlStyles.ResizeRedraw, false);
			if (myPicture != null)
			{			
				this.AutoScroll = true;
				this.AutoScrollMinSize = myPicture.Size;
				this.Invalidate();
			}
		}

		private void OnClose(Object mySender , EventArgs myArgs)
		{
			closePicture.Enabled = false;
			myPicture = null;
			myPicStyle = false;
			this.AutoScroll = false;
			this.Invalidate();		
		}

		private void OnExit(Object mySender , EventArgs myArgs)
		{
			this.Close();
		}	

		private void OnOpen(Object mySender , EventArgs myArgs)
		{	
			if (myPicture != null)
			{
				
			}
			OpenFileDialog myDialog = new OpenFileDialog();
			myDialog.Filter = "Image Files (JPEG,GIF,BMP)|*.jpg;*.jpeg;*.gif;*.bmp|JPEG Files(*.jpg;*.jpeg)|*.jpg;*.jpeg|GIF Files(*.gif)|*.gif|BMP Files(*.bmp)|*.bmp";

			if (myDialog.ShowDialog() == DialogResult.OK)
			{
				String myFileName = myDialog.FileName;
				if (myFileName.Length != 0)
				{
					try
					{					
						closePicture.Enabled = true;
						myPicture = new Bitmap(myFileName);
						this.Text = "Picture Viewer - " + myFileName;
						this.AutoScroll = true;
						this.AutoScrollMinSize = myPicture.Size;		
						this.Invalidate();
					}
					catch
					{
						MessageBox.Show(String.Format("{0} is not a valid Image File" , myFileName) , "Picture Viewer" , MessageBox.OK | MessageBox.IconError);
					}
				}		
			}
		}

		public static void Main(string[] args)
		{
			Application.Run(new PictViewForm());
		}
	}
}