home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Dialog Boxes / ImageOpen / ImageOpen.cs next >
Encoding:
Text File  |  2001-01-15  |  2.4 KB  |  70 lines

  1. //----------------------------------------
  2. // ImageOpen.cs ⌐ 2001 by Charles Petzold
  3. //----------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Windows.Forms;
  8.  
  9. class ImageOpen: Form
  10. {
  11.      protected string strProgName;
  12.      protected string strFileName;
  13.      protected Image  image;
  14.  
  15.      public static void Main()
  16.      {
  17.           Application.Run(new ImageOpen());
  18.      }
  19.      public ImageOpen()
  20.      {
  21.           Text = strProgName = "Image Open";
  22.           ResizeRedraw = true;
  23.  
  24.           Menu = new MainMenu();
  25.           Menu.MenuItems.Add("&File");
  26.           Menu.MenuItems[0].MenuItems.Add(new MenuItem("&Open...", 
  27.                                    new EventHandler(MenuFileOpenOnClick),
  28.                                    Shortcut.CtrlO));
  29.      }
  30.      void MenuFileOpenOnClick(object obj, EventArgs ea)
  31.      {
  32.           OpenFileDialog dlg = new OpenFileDialog();
  33.  
  34.           dlg.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;" +
  35.                               "*.jfif;*.png;*.tif;*.tiff;*.wmf;*.emf|" +
  36.                        "Windows Bitmap (*.bmp)|*.bmp|" +
  37.                        "Windows Icon (*.ico)|*.ico|" +
  38.                        "Graphics Interchange Format (*.gif)|*.gif|" +
  39.                        "JPEG File Interchange Format (*.jpg)|" +
  40.                               "*.jpg;*.jpeg;*.jfif|" +
  41.                        "Portable Network Graphics (*.png)|*.png|" +
  42.                        "Tag Image File Format (*.tif)|*.tif;*.tiff|" +
  43.                        "Windows Metafile (*.wmf)|*.wmf|" +
  44.                        "Enhanced Metafile (*.emf)|*.emf|" +
  45.                        "All Files (*.*)|*.*";
  46.  
  47.           if (dlg.ShowDialog() == DialogResult.OK)
  48.           {    
  49.                try
  50.                {
  51.                     image = Image.FromFile(dlg.FileName);
  52.                }
  53.                catch (Exception exc)
  54.                {
  55.                     MessageBox.Show(exc.Message, strProgName);
  56.                     return;
  57.                }
  58.                strFileName = dlg.FileName;
  59.                Text = strProgName + " - " + Path.GetFileName(strFileName);
  60.                Invalidate();
  61.           }
  62.      }
  63.      protected override void OnPaint(PaintEventArgs pea)
  64.      {
  65.           Graphics grfx = pea.Graphics;
  66.  
  67.           if (image != null)
  68.                grfx.DrawImage(image, 0, 0);
  69.      }
  70. }