public byte[] convertImageToByteArray(System.Drawing.Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
private void button1_Click(object sender, EventArgs e)
{
byte[] imgbnry = convertImageToByteArray(pictureBox1.Image);
string str = "insert into tbl_img (id,photo)values('" + textBox1.Text + "',@img)";
string ConStr = @"Server=COMP7;Database=ImageTest;User Id=sa;Password=cos123";
SqlConnection con = new SqlConnection(ConStr);
con.Open();
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("@img", imgbnry);
int n = cmd.ExecuteNonQuery();
if (n > 0)
{
MessageBox.Show("Successfully inserted");
}
else
{
MessageBox.Show("Not inserted");
}
}
Collapse | Copy Code
private void button2_Click(object sender, EventArgs e)
{
string str = "select photo from tbl_img where id='" + textBox2.Text + "'";
string ConStr = @"Server=COMP7;Database=ImageTest;User Id=sa;Password=cos123";
SqlConnection con = new SqlConnection(ConStr);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(str, con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
byte[] imgdata = new byte[0];
imgdata = (byte[])dt.Rows[0][0];
MemoryStream ms = new MemoryStream(imgdata);
pictureBox2.Image = Image.FromStream(ms);
}
else
{
MessageBox.Show("No images in a table");
}
}
use image data type to store image at sql server.
attach this table to crystal report.