Saving and loading image files

I have a listbox(lstContacts) that displays my contacts. When I choose a contact from the list and then click "View" button , data related to him/her appears on left side of screen. On the left side of the screen, there's also an imagebox that displays his/her picture in an imagebox called picPhoto. Under that imagebox, button"Edit Picture" does the following.

 With dlgChoosePic
            .Filter = "Jpeg Files (*.jpg)|*.jpg|Gif Files (*.gif)|*.gif)|Bitmap Files (*.bmp)|*.bmp)"
            .Title = "Choose an image to use"
            If .ShowDialog <> DialogResult.OK Then Return

            If .FileName <> "" Then
                picPhoto.Image = Image.FromFile(.FileName)
            End If
        End With

As you'll see, i want user to be able to choose bmp's, jpeg's and gif's. But this does not save the image. How will i make it so that, when a contact's picture is selected after clicking "edit picture", program will pop up a message box asking "are you sure you want to use this picture as contact's display picture?" and when user clicks yes, it will automatically save the picture.
Later, when a contact is selected and "View" button is clicked, his/her picture in imagebox will appear automatically.
I'd really appreciate if you could give exact code for my need.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Right.  Adding imagePath to the structure would work just fine.

Then, when you load the data for that contact, you can check the imagePath value.  If it is not blank, then load the image in the same way as you did from the "Edit Picture" button, except that you use your stored image fileName instead of the one selected by the user from the dialog.

Mike TomlinsonMiddle School Assistant TeacherCommented:
>> When I choose a contact from the list and then click "View" button , data related to him/her appears on left side of screen.

You need to store the path to the image (dlgChoosePic.FileName) in the structure that holds the rest of the data for that contact.

Where and how is this data stored?

aeustaAuthor Commented:
Structure Contact

        Dim LName As String
        Dim FName As String
        Dim Category As String
        Dim Company As String
        Dim HPhone As String
        Dim WPhone As String
        Dim FPhone As String
        Dim MPhone As String
        Dim Email As String
        Dim MSN As String
        Dim AIM As String
        Dim Web As String
        Dim HAddress As String
        Dim HCity As String
        Dim HState As String
        Dim HZip As String
        Dim HCountry As String
        Dim WAddress As String
        Dim WCity As String
        Dim WState As String
        Dim WZip As String
        Dim WCountry As String

    End Structure  

This is the structure for contact. So far, i did nothing to save the image nor path to the image. I guess i need to say
Dim imagePath as String
into the structure?
I'm kinda confused, never dealt with images before.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Mike TomlinsonMiddle School Assistant TeacherCommented:
>> I'm kinda confused, never dealt with images before.

With this approach you won't be saving the actual image in your structure, but only the full path and filename of the image.

I think you also want to save the image is it ??
May be when user selects the image and says OK yuo would like to save the image in a separate location and just store the name in the structure.

You can use picPhoto.Image.Save(Path) to save the image

aeustaAuthor Commented:
Idle Mind,
That's a smart way actually but i want the program to save the picture.

Arif Eqbal,
I tried : picPhoto.Image.Save("..\"), it gave following error.
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll

Additional information: A generic error occurred in GDI+.

Then i tried, picPhoto.Image.Save("..\pic") beucase tooltip says "inside paranthesis, i must write filename as String" , but this code does nothing, it does not work.
I would be surprised if it was this easy to save a file btw.
aeustaAuthor Commented:
I dont know if you guys use MSN messenger but what i exactly want is the "display picture" feature of msn messenger but without internet stuff.
When i click "edit picture" button, I should be able to browse folders, choose a picture, and the picture i chose must automatically appear in imagebox everytime i load contact's info.
I hope i'm clear enough.
aeustaAuthor Commented:
Idle Mind,
I gave up adding that function but gave you the points for your efforts.
All Courses

From novice to tech pro — start learning today.