Sijo George

Sijo George

  • NA
  • 31
  • 1.3k

Show image in picturebox using path saved in database

Apr 19 2018 5:13 AM
I got a table 'IMAGES' with fields 'img_username' , 'img_image_path. From my form I can upload image path to database and copy the image to a specific folder. Now I want to show the image in a pictureboxaccording the username I am giving. I will share the code so far iwritten. Please help. Now am getting error when I try to show image by giving a username.An unhandled exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll... This is the error showing at line 49. Is there any better way to do it?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Configuration;  
  11. using System.Data.SqlClient;  
  12. using System.IO;  
  13.   
  14. namespace ImageSaving  
  15. {  
  16.     public partial class Form1 : Form  
  17.     {  
  18.         SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings.Get("connectionstring").ToString());  
  19.         string displayimg, filepath;  
  20.         string folderpath = @"D:\Images\";  
  21.         OpenFileDialog open = new OpenFileDialog();  
  22.         SqlCommand cmd;  
  23.         public Form1()  
  24.         {  
  25.             InitializeComponent();  
  26.         }  
  27.   
  28.   
  29. //show image button  
  30.   
  31.         private void btn_ShowImage_Click(object sender, EventArgs e)  
  32.         {  
  33.             string sql = "select * from images where img_username=@username";  
  34.               
  35.             SqlCommand cmdrtrv = new SqlCommand(sql, con);  
  36.             cmdrtrv.Parameters.AddWithValue("@username", txt_UserName.Text);  
  37.              
  38.             try  
  39.             {  
  40.                 if (con.State == ConnectionState.Closed)  
  41.                 {  
  42.                     con.Open();  
  43.                 }  
  44.                 using (SqlDataReader reader = cmdrtrv.ExecuteReader())  
  45.                 {  
  46.                     if (reader.Read())  
  47.                     {  
  48.                         string path = @reader["img_path"].ToString();  
  49.                         pctbx_UserImage.Image = Image.FromFile("" + reader["path"].ToString());  
  50.                     }  
  51.                 }  
  52.                   
  53.             }  
  54.             finally  
  55.             {  
  56.                 con.Close();  
  57.             }  
  58.   
  59.         }  
  60.   
  61.         private void Form1_Load(object sender, EventArgs e)  
  62.         {  
  63.             con.Open();  
  64.              
  65.         }  
  66. //Search image button for upload  
  67.   
  68.         private void btn_SearchImage_Click(object sender, EventArgs e)  
  69.         {  
  70.             open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp|all files|*.*";  
  71.             if(open.ShowDialog()==DialogResult.OK)  
  72.             {  
  73.                 displayimg = open.SafeFileName;  
  74.                 txt_ImagePath.Text = open.FileName;  
  75.                 //Bitmap img = new Bitmap(open.FileName);  
  76.                 pctbx_UserImage.Image = new Bitmap(open.FileName);  
  77.                 filepath = open.FileName;  
  78.             }  
  79.         }  
  80.   
  81. //Upload button  
  82.   
  83.         private void btn_Upload_Click(object sender, EventArgs e)  
  84.         {  
  85.             if(filepath=="")  
  86.             {  
  87.                 cmd.Parameters.AddWithValue("@Path""");  
  88.             }  
  89.             else  
  90.             {  
  91.                   
  92.                 cmd = new SqlCommand("insert into images values(@UserName,@Path)",con);  
  93.                 cmd.CommandType = CommandType.Text;  
  94.                 cmd.Parameters.AddWithValue("@UserName", txt_UserName.Text);  
  95.                 cmd.Parameters.AddWithValue("@Path", folderpath+Path.GetFileName(open.FileName));  
  96.                 File.Copy(filepath, Path.Combine(folderpath, Path.GetFileName(open.FileName)),true);  
  97.                 if(con.State==ConnectionState.Closed)  
  98.                 {  
  99.                     con.Open();  
  100.                 }  
  101.                 cmd.ExecuteNonQuery();  
  102.                 MessageBox.Show("Image Saved");  
  103.                 con.Close();  
  104.             }  
  105.         }  
  106.     }  
  107. }  

Answers (1)