Download presentation
Presentation is loading. Please wait.
1
How to Add Images Using an 'openFile' Dialog
A Guide in 9 Steps (and abusing David Beckham in the process)
2
Creating Controls 1. Create a new PictureBox control
2. Create a new OpenFileDialog control (Note: although you drag it onto the form designer it does not show up on the form but in the area below the form designer)
3
Creating Defaults 3. In the Properties Manager add a background image with some info for the user how to add an image. (Note: create the image in, say, MS Paint with the same dimensions as the PictureBox)
4
Creating Defaults 4. In the Properties Manager set the image sizing behaviour to 'StretchImage" (otherwise all you images have to be of the same size as the pictureBox) 5. In the Properties Manager set the image type filter, e.g. to: JPG files |*.jpg|Bitmap files|*.bmp (Note: the vertical bars are required) 6. In the Properties Manager delete the default FileName and InitialDirectory (or set them to a default of your liking) All visual controls are now in place. Let's add some code…
5
Defining a Variable 7. Define a string variable to hold a pathname to the image public string ImgFilePathName; ImgFilePathName = ""; // You could set it to a default here
6
Adding new Images 8. Double-click on the PictureBox to create the event handler stub. Then add this code: private void pictureBox1_Click(object sender, EventArgs e) { openFileDialog1.Title = "Select an image file"; if (openFileDialog1.ShowDialog() == DialogResult.OK) ImgFilePathName = openFileDialog1.FileName; pictureBox1.ImageLocation = ImgFilePathName; } This line saves the file's path name This line shows the image
7
In the “Database” Project
9. You probably want to store the image in the “database”. The easiest way is not to store the image but just the filename. Add a string to the struct, e.g: public string imageFileName; In the OpenFile dialog code add 3 lines to store the filename: private void pictureBox1_Click(object sender, EventArgs e) { openFileDialog1.Title = "Select an image file"; if (openFileDialog1.ShowDialog() == DialogResult.OK) ImgFilePathName = openFileDialog1.FileName; pictureBox1.ImageLocation = ImgFilePathName; Member temp = Club[currentMemberShown]; temp.imageFileName = ImgFilePathName; Club[currentMemberShown] = temp; } THAT'S IT !
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.