You are on page 1of 7
IMAGE PROCESSING WITH DATABASE 1) Go to All programs Microsoft Visual Studio 2008 2) FileNew project 3) Give the new name to the creating project and save it in corresponding position 4) Design the winform ADDING TAB: place the picture box and two buttons. VIEW TAB: place the picture box and data grid view. 5) Add “using System.Data.SqlClient;” as header file NOTE: Data Base Connectivity SqlConnection : It’s a string,which is used to connect the front end with data base SqlCommand : This is used to execute the process of data insertion,deletion and update sql queries SqlDataAdapter: it is used to data fetching like select sql querie. "server=server name;uid=username;pwd=password;database=database name"  Server name: MS-SQL server name and we can give it as “.”  Username: user account name of MS-SQL server.  Password: Login password  Database name: data base used for this project *)username and passowrd have to change in the accounding according to your server 6) Declare the variables globally string imagename; SqlConnection con = new SqlConnection(); SqlCommand cmd = new SqlCommand(); SqlDataAdapter adp = new SqlDataAdapter(); 7) code for “Browse” button click try { FileDialog fldlg = new OpenFileDialog(); fldlg.InitialDirectory = @":D\"; fldlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif"; if (fldlg.ShowDialog() == DialogResult.OK) { imagename = fldlg.FileName; Bitmap newimg = new Bitmap(imagename); pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1.Image = (Image)newimg; } fldlg = null; } catch (System.ArgumentException ae) { imagename = " "; MessageBox.Show(ae.Message.ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } 8) code for “Submit” button click if (MessageBox.Show("are u sure,want to save?", "staff information", MessageBoxButtons.YesNo) == DialogResult.Yes) { inserting(); } Application.Restart(); Inserting Function used for submit process public void inserting() { try { if (imagename != "") { FileStream fs; fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read); byte[] picbyte = new byte[fs.Length]; fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length)); fs.Close(); string connstr = @"server=.;uid=prabhakar;pwd=welcome;database=image"; SqlConnection conn = new SqlConnection(connstr); conn.Open(); string query; query = "insert into picture(pict) values(" + " @pic)"; SqlParameter picparameter = new SqlParameter(); picparameter.SqlDbType = SqlDbType.Image; picparameter.ParameterName = "pic"; picparameter.Value = picbyte; SqlCommand cmd = new SqlCommand(query, conn); cmd.Parameters.Add(picparameter); cmd.ExecuteNonQuery(); MessageBox.Show("DATA ENTERD SUCCESSFULLY"); cmd.Dispose(); conn.Close(); conn.Dispose(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } VIEW TAB: 9)Grid View Methods The actions involed in grid are three methods  Click()  MouseClick()  SelectionChanged() These three methods should call the user defined function gridselection(); User defined function gridselection() public void gridselection() { try { int i = dataGridView1.CurrentRow.Index; int x = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value); adp = new SqlDataAdapter("select pict from picture where numb='" + x + "'", con); DataSet ds = new DataSet(); DataTable dt = new DataTable(); adp.Fill(ds, "picture"); dt = ds.Tables[0]; if (dt.Rows.Count > 0) { byte[] byt = (byte[])dt.Rows[0][0]; MemoryStream ms = new MemoryStream(byt); Bitmap bmp1 = new Bitmap(ms); pictureBox2.Image = bmp1; } } catch (Exception es) { } } 10) code for connecting with date during form load con = new SqlConnection("server=.;uid=prabhakar;pwd=welcome;database=image"); con.Open(); adp = new SqlDataAdapter("select numb from picture", con); DataSet ds = new DataSet(); DataTable dt = new DataTable(); adp.Fill(ds, "picture"); dt = ds.Tables[0]; dataGridView1.DataSource = dt; con.Close(); 11) create the data base and table in the ms-sql DB name: image Table name: picture Table attributes are 1) numb(int), auto increase, primary key 2)pict (image). OUTPUT: