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
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:
To save the image:
Create a new instance of SaveFileDialog:
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.
Stay tuned for a tutorial on how to use ColorDialog and FontDialog.
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
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'.
Dim OFD As New OpenFileDialog
Code: Select all
Then the DialogResult is declared as 'rslt' to check which button on the OpenFileDialog was pressed when it was opened.
OFD.Filter = "All Picture Files |*.gif;*.jpg;*.jpeg;*.jpe;*.jfif;*.png;*.bmp;*.dib;*.wmf;*.art;*.ico|All Files (*.*)|*.*"
Code: Select all
If the OK button was pressed, then the selected image (filename) will be loaded into the PictureBox.
Dim rlst As DialogResult = OFD.ShowDialog()
Code: Select all
If the code works fine, then the selected image will be loaded into the PictureBox1.If rslt = DialogResult.OK Then
PictureBox1.Image = Bitmap.FromFile(OFD.FileName)
End If
To save the image:
Create a new instance of SaveFileDialog:
Code: Select all
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.
Dim SFD As New SaveFileDialog
Code: Select all
Now again, declare the DialogResult as rslt, which tells the app which button was pressed when the dialog was displayed:
SFD.Filter = "PNG |*.png"
Code: Select all
[/code][code]Dim rlst As DialogResult = SFD.ShowDialog()
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 the code was successful then the image will be saved in the selected directory.If rslt = DialogResult.OK Then
PictureBox1.Image.Save(SFD.FileName, System.Drawing.Imaging.ImageFormat.Png)
End If
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.
Good tutorial, Keep them coming 
Its incredible how many people forget to add the filters or check the dialog result. Even in professional apps.

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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023