This is the third part of this tutorial series. In this part I will show you how to browse for image in local storage , how to get properties of that image and how to save it to database. I will try to describe every single part in details. It will help you to learn and understand properly.
## Browse local storage for image
Double click on Browse button ("btnBrowse" is my browse button's name) to get click event for this button.(You already know that).
To browse local storage for any file we have to use a control named "OpenFileDialog" . You can just drag and drop it from toolbox(Toolbox >> Dialogs >> OpenFileDialog) to your form. Or you can declare a new instance of OpenFileDialog control. I prefer to use the second option. (This will give you better control over your code).
OpenFileDialog dlg = new OpenFileDialog();
Now we will filter the file as we need only image files. We will filter on .JPG .PNG AND .BMP images.
dlg.Filter = "Image File (*.jpg;*.bmp;*.png;)|*.jpg;*.bmp;*.png;";
Then we have to collect the dialog result.
And at last we will can add this picture in PictureBox and collect required properties and show the properties in textbox. And later we will save the name , location and image in database. OpenFileDialog.FileName provied the file's location and OpenFileDialog.SafeFileName provides only the file's name and it's extension.
## Browse local storage for image
Double click on Browse button ("btnBrowse" is my browse button's name) to get click event for this button.(You already know that).
To browse local storage for any file we have to use a control named "OpenFileDialog" . You can just drag and drop it from toolbox(Toolbox >> Dialogs >> OpenFileDialog) to your form. Or you can declare a new instance of OpenFileDialog control. I prefer to use the second option. (This will give you better control over your code).
OpenFileDialog dlg = new OpenFileDialog();
Now we will filter the file as we need only image files. We will filter on .JPG .PNG AND .BMP images.
dlg.Filter = "Image File (*.jpg;*.bmp;*.png;)|*.jpg;*.bmp;*.png;";
Then we have to collect the dialog result.
DialogResult dlgRes = dlg.ShowDialog();
And at last we will can add this picture in PictureBox and collect required properties and show the properties in textbox. And later we will save the name , location and image in database. OpenFileDialog.FileName provied the file's location and OpenFileDialog.SafeFileName provides only the file's name and it's extension.
if (dlgRes != DialogResult.Cancel)
{
//Set
image in picture box
pictureBox1.ImageLocation =
dlg.FileName;
//Provide
file path in txtImagePath text box.
txtImagePath.Text =
dlg.FileName;
txtFileName.Text =
dlg.SafeFileName;
}
No comments:
Post a Comment