Save and Retrieve Image

Oct 20 2009 12:29 PM
It's me again. Am writing this code that allows the user to select an image which is saved into a folder. However, I want the selected image file name to be saved also so that I can load it later into a picture box called MemberPics. Please help me. I am able to select an image but the filename is not the one which is save. Please below is the code for selecting an image.

 private void btnpics_Click(object sender, EventArgs e)
        {
OpenFileDialog openFD = new OpenFileDialog();
            //string chosen_file = "";

            openFD.Title = "Insert an image ";
            openFD.InitialDirectory = "c:";
            openFD.FileName = "";
            openFD.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png";


            if (openFD.ShowDialog() == DialogResult.Cancel)
            {
                MessageBox.Show("Operation cancelled !");
            }

            else
            {
                chosen_file = openFD.FileName;
                MemberPics.Image = Image.FromFile(chosen_file);
                MemberPics.Image.Save("d:\\Pictures\\chosen_file", System.Drawing.Imaging.ImageFormat.Jpeg);
            }
}

below is the code i want to use to load the image later into another picture box called display

display.Image = Image.FromFile(@"d:\\Pictures\\chosen_file.jpg");

The problem is am not able to save the image with the FileName property of the openDialog so i find it difficult to load the image into the display picturebox. please help me with how to use the FileName property of openDialog so that i can load different images into the display later.


Answers (16)