Saving and loading image files

Posted on 2004-11-01
Last Modified: 2010-04-23
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.
Question by:aeusta
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    >> 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?


    Author Comment

    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.
    LVL 85

    Accepted Solution

    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.

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    >> 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.

    LVL 19

    Expert Comment

    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


    Author Comment

    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.

    Author Comment

    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.

    Author Comment

    Idle Mind,
    I gave up adding that function but gave you the points for your efforts.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now