Thursday, November 26, 2009

To Store and Retrieval Image in Windows Application

****************************************************************
Upload the Image in SqlServer2005

SqlConnection con = new SqlConnection("server=vivek\\server2005;initial catalog=TESTIMAGE;integrated security=true");
con.Open();
SqlCommand cmd = new SqlCommand("IMG_INSERT", con);
cmd.Parameters.AddWithValue("@id", textBox2.Text);
//////////////////////
MemoryStream stream = new MemoryStream();
pictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = stream.ToArray();
cmd.Parameters.AddWithValue("@img", pic);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
MessageBox.Show("Sucessfully Saved");


*************************************************************************
Download and Display in to Picture using Timer

//Global Declaration

private string s = ConfigurationSettings.AppSettings["conn"];
DataSet ds = new DataSet();
static int cnt = 0;

//Form Load Coding

private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(s);
con.Open();
SqlDataAdapter ada = new SqlDataAdapter("SELECT studid,PHOTO FROM studentregistration", con);
ada.Fill(ds);
}

//Timer Coding


private void timer1_Tick(object sender, EventArgs e)
{
Bitmap map ;
if (cnt < ds.Tables[0].Rows.Count)
{
byte[] bytes = (byte[])ds.Tables[0].Rows[cnt][1];
MemoryStream mem = new MemoryStream(bytes);
map = new Bitmap(mem);
pictureBox1.Image = map;
cnt += 1;

}
else
{
cnt = 0;
}
}