Hey There!
Im Looking For A Code To Select An Image File And Put It Into An Image Box. It Should Be Fairly Simple. Thanks
Ben
Printable View
Hey There!
Im Looking For A Code To Select An Image File And Put It Into An Image Box. It Should Be Fairly Simple. Thanks
Ben
I'm not going to give you the code but rather guide you how to get it done... That way, you'll have a chance to learn.
To allow the user to browse for a file, you'd use the OpenFileDiag control. To display the openfiledialog, you call its ShowDialog method. The openfiledialog returns a DialogResult value, which tells you what button on the openfiledialog was clicked by the user (OK or Cancel...). OpenFileDialog.Filter property allows you to specify a filter such that only certain file types will be displayed. The FileName property gives you the full path to the file selected by the user. Once you have the file path, you can use Image.FromFile(path) to create an Image object and then assign it to your PictureBox.Image property.
Psuedo-code:
- Create an OpenFileDialog object (can be dropped from the toolbox to your form in design view)
- Upon an event (such as the click of a button), you show the openfiledialog
- Get the returned result, and if it's the Dialogresult.OK then you read the file name from FileName property of the OFD.
- Create an Image using Image.FromFile(fileName)
- Assign the image to PictureBox.Image property
That's all there is to it. And happy coding :)
As I Am New To This Could You Please Give Me The Coding.
I Can Do Most FO The Steps But It Doesnt Seem To Piece Togther
Thanks
Ben
You won't learn anything if everything is coded for you by someone else...
But here it is. Put this code where you want to display the openfiledialog, i.e. in a button click event handler...
Code:Using ofd As New OpenFileDialog()
With ofd
.Filter = "Image Files|*.jpg;*.bmp;*.png;*.gif"
.Multiselect = False
.CheckFileExists = True
If .ShowDialog = Windows.Forms.DialogResult.OK Then
Dim img As Image = Image.FromFile(.FileName)
PictureBox1.Image = img
End If
End With
End Using
Thanks Much! I Am Slowloy Learning!
As This Is A User Interpretd Program Is There Anyway I Could Make The Image Stay The Same Everytime The Program Is Started, Unless It Is Changed. Thanks! It Wouyld Be G00d If This Was Coding Aswell!
Thnaks Again!
Ben
Have a look at My.Settings... Basically you declare a setting of type String with user scope, and when your program loads, you pull that setting from My.Settings. If it is string.empty, you let the user pick an image, then load the image to your picturebox and you save the path to that image into My.Settings, else, you check to see the file exists and load the image using the path from My.Settings.
Thanks! Youve Been A Good Help!
The Code Unfoturaltly Doesn't Work....I Am Struggling As I Am Only 13 years Old! It Doesnt Seem To Pull The String :(
Could I HAve Some Help Pelase!
Thanks
Follow these steps:
A. Creating a setting:
1. Click Project in the menu bar, and choose [your project name] Properties...
2. Click on the Settings tab
3. Type in a new setting as followings:
Name: LastImageFilePath
Type: String
Scope: User
Value: leave it blank
4. Save and close the application property window/tab.
B: Read the setting
In your form.load event handler:
C: Save the setting:Code:Dim imagePath As String = My.Settings.LastImageFilePath
If Not String.IsNullOrEmpty(imagePath) Then
If System.IO.File.Exists(imagePath) Then
PictureBox1.Image = Image.FromFile(imagePath)
End If
End If
Suppose the user want to pick an image file, you show the openfiledialog as shown in my previous post. You just need to add code to save the image path (in rerd)
And that's all there is to it.Code:If .ShowDialog = Windows.Forms.DialogResult.OK Then
My.Settings.LastImageFilePath = .FileName
My.Settings.Save()
Dim img As Image = Image.FromFile(.FileName)
PictureBox1.Image = img
End If
Thanks It Works Perfectly!