using System; using System.Drawing; using System.Windows.Forms; class App{ public static void Main(){ Application.Run(new ViewForm()); } } class ViewForm:Form{ public ViewForm(){ MenuItem file = new MenuItem("&File"); file.MenuItems.Add("Open", new EventHandler(OnOpen)); file.MenuItems.Add("-"); file.MenuItems.Add("Exit", new EventHandler(OnExit)); this.Menu = new MainMenu(new MenuItem[]{file}); this.Text = "MDI Image Viewer"; this.IsMdiContainer = true; } void OnOpen(Object sender, EventArgs args){ OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Image Files (JPEG, GIF, BMP, etc.)|" + "*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.png|" + "All files (*.*)|*.*"; if (ofd.ShowDialog() == DialogResult.OK) { String fileName = ofd.FileName; if (fileName.Length != 0) { try { Form bitmap = new BitmapForm(fileName, this); }catch(ArgumentException e) { MessageBox.Show(String.Format( "{0} is not a valid image file", fileName)); } } } } void OnExit(Object sender, EventArgs args){ this.Close(); } } class BitmapForm:Form{ Bitmap bitmap; public BitmapForm(String filename, Form parent){ bitmap = new Bitmap(filename); if(parent != null){ this.MdiParent = parent; } this.ResizeRedraw = true; this.Text = filename; this.Show(); } protected override void OnPaint(PaintEventArgs args){ base.OnPaint(args); args.Graphics.DrawImage(bitmap, this.ClientRectangle); } }