Powered by Blogger.

Sunday, March 2, 2014

OpenFileDialog in C#.Net windows application



In this post we will discuss about OpenFileDialog in C#.Net windows application. Also you can check out my previous posts on:

- Error functions in sql server

- Triggers in sql server 2008

- Identity in sql server 2008

OpenFileDialog class provides a dialog box that enables the user to select one or more files.

OpenFileDialog allows users to browse folders and select files. It is available in Windows Forms and can be used with C# code. It displays the standard Windows dialog box. The results of the selection can be read in your C# code.

In this example we will see whenever a user will select an image though the OpenFileDialog, the image will appear in the picture box.

So First Drag and drop a picture box and a button to the form. Then in the button click event write the below code.

  private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Open Photo";
            dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(dlg.OpenFile());
            }
            dlg.Dispose();
        }

Once user select an image, the image will appear in the picture box as shown in the fig below:



0 comments

Post a Comment