Thursday, February 11, 2010

ImageStoring and Retrieval In Windows Application Using C#

// Storing of the Image //

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();

// Retrieval of the Image //

SqlDataAdapter ada = new SqlDataAdapter(cmd);
ds = new DataSet();
ada.Fill(ds);
byte[] bytes = (byte[])ds.Tables[0].Rows[0][1];
MemoryStream mem = new MemoryStream(bytes);
Bitmap map = new Bitmap(mem);
pictureBox1.Image = map;


// Display Using Timer Values //


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;
}
}

No comments:

Post a Comment