Reading Picture from directory into Windows Form

In this article I am going to explain how we can access the pictures which are stored in some directory in our system and filename is stored in database.

For example we have directory named as "dotnet test folder" where our pictures are kept and the filename of these pictures are stored in database .In my system location of "dotnet test folder" is "F:\dotnet test folder", you can have location of your own choice.

Below are the pictures that are in directory and their filenames are stored in table named as "blog_pic" in database.

4-cute.jpg

24eea8e395a5ca09eb0e6a70670094e8.jpg

image-4d0708ca748ee-candidfour18_620.jpg

image-4d0708f5a8432-candidfour11_620.jpg

221698_10150571350735714_506595713_18405405_404939.jpg

So what we are exactly doing here is accessing picture from directory which in my case is "dotnet test folder" and accessing its filename from the table in database.

So before we begin we need to have table in database , so we will create table in database with one field and insert the filename of picture in that table. Creation of table I have already explained in previous articles.

Second we would require a stored procedure for select statement for selecting the filename of pictures from table, code is given below:

CREATE PROCEDURE [dbo].[blog_select_picture]

(

@msg varchar(100) output

)

    -- Add the parameters for the stored procedure here

    

AS

BEGIN try

    

    SET NOCOUNT ON;

select * from blog_pic;

 

return 0

END try

begin catch

select @msg='Error!!!Problem Occured During Process'

return 1

end catch

C # code:

The above is the windows form here we have two button next>> and <<previous, by clicking on next >> button we are moving forward that is we are reading picture which is stored in directory and getting its filename from database to populate it to the picture box in form and by clicking on <<previous button we are reading in reverse.

To put it into simplest form we are doing:

Directory (were actual picture are stored) + table in database (filename) = populating picture in form.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Data.SqlClient;

namespace blog

{

public partial class Form4_select_picture : Form

{

SqlConnection con;

string constr = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=blog;Data Source=MY-PC\SQLEXPRESS";

string sp_e_msg;

SqlCommand cmd;

SqlDataAdapter adpt;

DataTable dt;

int proc = 0;

string notific;

public Form4_select_picture()

{

InitializeComponent();

}

 

private void Form4_select_picture_Load(object sender, EventArgs e)

{

 

}

//*************************next>>**************************************

private void button1_Click(object sender, EventArgs e)

{

try

{

con = new SqlConnection();

con.ConnectionString = constr;

con.Open();

cmd = new SqlCommand("blog_select_picture", con);

cmd.CommandType = CommandType.StoredProcedure;

//***************************************************************************

SqlParameter parm2 = new SqlParameter("@returnval", SqlDbType.Int);

parm2.Size = 50;

parm2.Direction = ParameterDirection.ReturnValue;

cmd.Parameters.Add(parm2);

//**************************************************************

SqlParameter parm1 = new SqlParameter("@msg", SqlDbType.VarChar);

parm1.Size = 50;

parm1.Direction = ParameterDirection.Output;

cmd.Parameters.Add(parm1);

//***************************************************************

adpt = new SqlDataAdapter(cmd);

dt = new DataTable();

adpt.Fill(dt);

if (dt.Rows.Count > 0)

{

 

label1.Text = dt.Rows[proc]["picture"].ToString();

//**********we are getting picture from F:\dotnet test folder\ and getting //filename from database to label1. Combining the two we are getting picture //in picture box

pictureBox1.ImageLocation = @"F:\dotnet test folder\" + label1.Text;

proc = proc + 1;

 

cmd.ExecuteNonQuery();

notific = cmd.Parameters["@returnval"].Value.ToString();

//*************************************************************************

sp_e_msg = cmd.Parameters["@msg"].Value.ToString();

 

}

 

}

 

catch (Exception ex)

{

label1.Text = ex.ToString();

if (notific == "1")

{

label1.Text = sp_e_msg + notific+ex.ToString ();

}

}

finally

{

con.Close();

con.Dispose();

cmd.Dispose();

 

if (proc > dt.Rows.Count - 1)

{

label1.Text = "Records Ends";

proc = dt.Rows.Count - 1;

}

}

}

//*****************************<<previous************************************

private void button2_Click(object sender, EventArgs e)

{

try

{

 

con = new SqlConnection();

con.ConnectionString = constr;

con.Open();

 

cmd = new SqlCommand("blog_select_picture", con);

cmd.CommandType = CommandType.StoredProcedure;

//**************************************************************************

SqlParameter parm2 = new SqlParameter("@returnval", SqlDbType.Int);

parm2.Size = 50;

parm2.Direction = ParameterDirection.ReturnValue;

cmd.Parameters.Add(parm2);

//**************************************************************

SqlParameter parm1 = new SqlParameter("@msg", SqlDbType.VarChar);

parm1.Size = 50;

parm1.Direction = ParameterDirection.Output;

cmd.Parameters.Add(parm1);

//***************************************************************

adpt = new SqlDataAdapter(cmd);

dt = new DataTable();

adpt.Fill(dt);

if (dt.Rows.Count > 0)

{

//**********we are getting picture from F:\dotnet test folder\ and getting //filename from database to label1. Combining the two we are getting picture //in picture box

 

label1.Text = dt.Rows[proc]["picture"].ToString();

pictureBox1.ImageLocation = @"F:\dotnet test folder\" + label1.Text;

proc = proc - 1;

cmd.ExecuteNonQuery();

notific = cmd.Parameters["@returnval"].Value.ToString();

//*************************************************************************

sp_e_msg = cmd.Parameters["@msg"].Value.ToString();

 

}

 

}

 

catch (Exception ex)

{

 

if (notific == "1")

{

label1.Text = sp_e_msg + notific;

}

 

}

finally

{

con.Close();

con.Dispose();

cmd.Dispose();

 

if (proc < 0)

{

label1.Text = "Records Ends";

proc = 0;

}

}

}

}

}

 

Tags: , ,

Windows Form