Wednesday 6 June 2012

OpenFileDialog

   private static void raiseOpenFileDialog()
        {
            try
            {
                Stream myStream = null;
                OpenFileDialog openFileDialog1 = new OpenFileDialog();

                openFileDialog1.InitialDirectory = "c:\\";

                openFileDialog1.Filter = "Picture (*.jpg,*.bmp,*.gif,*.png)|*.jpg;*.bmp;*.gif;*.png";
                openFileDialog1.FilterIndex = 4;
                openFileDialog1.Title = "Choose a picture";
                openFileDialog1.RestoreDirectory = true;

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        if ((myStream = openFileDialog1.OpenFile()) != null)
                        {
                            using (myStream)
                            {
                                // Insert code to read the stream here.
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }