Click here to Skip to main content
16,022,417 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am deveolping an application for our office, and in the employee data will include their picture(Passport Photograph)to be uploaded and the Asministrator may want to view the emloyee details showing the saved passport.
I want to see the C# code for:Uploading, Saving and displaying the Picture.
thanks.
Regads.
M.A. Busu
Posted

private void btnupload_Click(object sender, EventArgs e)
    {
      try
      {
        openFileDialog1.ShowDialog(this);
        openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
        
        byte[] image;
 
        string fileName =openFileDialog1.FileName;
 
        FileStream fs = new FileStream(fileName, FileMode.Open);
        BinaryReader reader = new BinaryReader(fs);
        image = reader.ReadBytes((int)fs.Length);
        fs.Close();
 
        SqlConnection conn = new SqlConnection("connectionString");
        conn.Open();
        string query = "Insert into pictures (name,images) values(@name,@images)";
 
        SqlCommand comm = new SqlCommand(query, conn);
        comm.Parameters.AddWithValue("name", Path.GetFileName(strFn).ToString());
 
        comm.Parameters.AddWithValue("images", image);
 
        comm.ExecuteNonQuery();
        MessageBox.Show("Record Added");
 
        conn.Close();
        bind();
        pictureBox1.Image = new Bitmap(strFn);
      
      }
      catch (Exception)
      {
        throw new ApplicationException("Failed loading image");
      }     
    }
 
Share this answer
 
v2
Comments
[no name] 24-Jun-11 6:26am    
Edited for code block.
Looks to me you are talking of winforms.
Have a look at this article to start of: C# Photo Album Viewer[^]
 
Share this answer
 
Comments
Tarun.K.S 12-Feb-11 4:52am    
Good link!
[no name] 24-Jun-11 6:24am    
Good Call My 5 too Sandeep.
Sandeep Mewara 24-Jun-11 14:56pm    
Thanks RK.
i would design a database that holds employee data. and for the the picture just add a field in the database that saves the path & filename to the picture. then bind a picturebox to that field

Database example
----------------------------------
Name: john doe
DOB: 04/04/04
address: blah blah
prof_pic: ~/picturepath/1.jpg
--------------------------

form_load event
{

pictureBox.imageUrl = prof_pic

}
 
Share this answer
 
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Feb-11 22:27pm    
Good starting point. A 5.
--SA
[no name] 24-Jun-11 6:24am    
Good Call. My 5 too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900