How to use FileDialogs

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
2 posts Page 1 of 1
Contributors
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

How to use FileDialogs
Usman55
I prefer to create new instances of FileDialogs whenever I need to use them because not only do they use less resources but the size of the resultant app is small as well.

In this tutorial, the use of OpenFileDialog and SaveFileDialog will be explained. Both of them work on almost the same principle, except their function is opposite.

In this example, we'll be using a PictureBox into which an image from the OpenFileDialog will be loaded and then the image will be saved using a SaveFileDialog.


To load an image:

First a new instance of an OpenFileDialog is created:
Code: Select all
Dim OFD As New OpenFileDialog
Next the filter of the dialog is chosen, depending on what is to be loaded into the app. If we don't set this property, it will show all files in the dialog. We're loading an image, so we'll set the filter to 'All Picture Files'.
Code: Select all
OFD.Filter = "All Picture Files |*.gif;*.jpg;*.jpeg;*.jpe;*.jfif;*.png;*.bmp;*.dib;*.wmf;*.art;*.ico|All Files (*.*)|*.*"
Then the DialogResult is declared as 'rslt' to check which button on the OpenFileDialog was pressed when it was opened.
Code: Select all
Dim rlst As DialogResult = OFD.ShowDialog()
If the OK button was pressed, then the selected image (filename) will be loaded into the PictureBox.
Code: Select all
If rslt = DialogResult.OK Then
                PictureBox1.Image = Bitmap.FromFile(OFD.FileName)
            End If
If the code works fine, then the selected image will be loaded into the PictureBox1.


To save the image:

Create a new instance of SaveFileDialog:
Code: Select all
Dim SFD As New SaveFileDialog
Set the filter to the required format. We're using .png in our case. For each format, the code for saving the image has to be written separately.
Code: Select all
SFD.Filter = "PNG |*.png"
Now again, declare the DialogResult as rslt, which tells the app which button was pressed when the dialog was displayed:
Code: Select all
[code]Dim rlst As DialogResult = SFD.ShowDialog()
[/code]

If the OK button was pressed then save the image from the picturebox to the dialog's selected destination (filename). The format of the image will be PNG. Change this ImageFormat to different formats to save in a particular format.
Code: Select all
If rslt = DialogResult.OK Then
            PictureBox1.Image.Save(SFD.FileName, System.Drawing.Imaging.ImageFormat.Png)
        End If
If the code was successful then the image will be saved in the selected directory.



Stay tuned for a tutorial on how to use ColorDialog and FontDialog.
Last edited by Usman55 on Tue Apr 01, 2014 12:28 pm, edited 1 time in total.
Image
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: How to use FileDialogs
XTechVB
Good tutorial, Keep them coming :)
Its incredible how many people forget to add the filters or check the dialog result. Even in professional apps.
You can find me on Facebook or on Skype mihai_92b
2 posts Page 1 of 1
Return to “Tutorials”