new SaveFileDialog() : SaveFileDialog « System.Windows.Forms « C# / C Sharp by API






new SaveFileDialog()

  
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
   
class ImageOpen: Form
{
     protected string strProgName;
     protected string strFileName;
     protected Image  image;
     MenuItem miSaveAs;
     public static void Main()
     {
          Application.Run(new ImageOpen());
     }
     public ImageOpen()
     {
          ResizeRedraw = true;
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&File");
          Menu.MenuItems[0].MenuItems.Add(new MenuItem("&Open...", 
                                   new EventHandler(MenuFileOpenOnClick),
                                   Shortcut.CtrlO));
          Menu.MenuItems[0].Popup += new EventHandler(MenuFileOnPopup);
          miSaveAs = new MenuItem("Save &As...");
          miSaveAs.Click += new EventHandler(MenuFileSaveAsOnClick);
          Menu.MenuItems[0].MenuItems.Add(miSaveAs);
     }
     void MenuFileOnPopup(object obj, EventArgs ea)
     {
          miSaveAs.Enabled = (image != null);
     }
     void MenuFileSaveAsOnClick(object obj, EventArgs ea)
     {
          SaveFileDialog savedlg = new SaveFileDialog();
   
          savedlg.InitialDirectory = Path.GetDirectoryName(strFileName);
          savedlg.FileName = Path.GetFileNameWithoutExtension(strFileName);
          savedlg.AddExtension = true;
          savedlg.Filter = "Windows Bitmap (*.bmp)|*.bmp|" +
                           "Graphics Interchange Format (*.gif)|*.gif|" +
                           "JPEG File Interchange Format (*.jpg)|" +
                              "*.jpg;*.jpeg;*.jfif|" +
                           "Portable Network Graphics (*.png)|*.png|" +
                           "Tagged Imaged File Format (*.tif)|*.tif;*.tiff";
   
          if (savedlg.ShowDialog() == DialogResult.OK)
          {
               try
               {
                    image.Save(savedlg.FileName);
               }
               catch (Exception exc)
               {
                    MessageBox.Show(exc.Message, Text);
                    return;
               }
               strFileName = savedlg.FileName;
               Text = strProgName + " - " + Path.GetFileName(strFileName);
          }
     }

     void MenuFileOpenOnClick(object obj, EventArgs ea)
     {
          OpenFileDialog dlg = new OpenFileDialog();
   
          dlg.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;" +
                              "*.jfif;*.png;*.tif;*.tiff;*.wmf;*.emf|" +
                       "Windows Bitmap (*.bmp)|*.bmp|" +
                       "Windows Icon (*.ico)|*.ico|" +
                       "Graphics Interchange Format (*.gif)|*.gif|" +
                       "JPEG File Interchange Format (*.jpg)|" +
                              "*.jpg;*.jpeg;*.jfif|" +
                       "Portable Network Graphics (*.png)|*.png|" +
                       "Tag Image File Format (*.tif)|*.tif;*.tiff|" +
                       "Windows Metafile (*.wmf)|*.wmf|" +
                       "Enhanced Metafile (*.emf)|*.emf|" +
                       "All Files (*.*)|*.*";
   
          if (dlg.ShowDialog() == DialogResult.OK)
          {    
   
               try
               {
                    image = Image.FromFile(dlg.FileName);
               }
               catch (Exception exc)
               {
                    Console.WriteLine(exc.Message);
                    return;
               }
               strFileName = dlg.FileName;
               Text = Path.GetFileName(strFileName);
               Invalidate();
          }
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
   
          if (image != null)
               grfx.DrawImage(image, 0, 0);
     }
     
}

   
    
  








Related examples in the same category

1.SaveFileDialog.CheckFileExists
2.SaveFileDialog.Filter
3.SaveFileDialog.InitialDirectory