First, use C # to convert image to byte[] and insert the database:
1.1 Convert Image control image to stream:
Copy Code code as follows:
Private byte[] Pictoarray ()
{
Bitmap BM = new Bitmap (picbox.image);
MemoryStream ms = new MemoryStream ();
Bm. Save (MS, Imageformat.jpeg);
Return Ms. GetBuffer ();
}
Copy Code code as follows:
Save to Database
Try
{
String sql = "Update t_employee set imagelogo= @ImageLogo where empid= @EmpId";
Sqlhelper.executenonquery (SQL, New SqlParameter ("@ImageLogo", Imgsourse));
MessageBox.Show ("Modify saved!") ");//Showinfo (0);
}
catch (Exception ex)
{
MessageBox.Show ("Update failed!") "+ Ex." message);
Return
}
1.2 Convert the picture file into a byte stream and insert the database:
Copy Code code as follows:
Class Imageinserter
{
public static int insertimg (string path)
{
----------read a picture in a file and convert it into a byte stream
FileStream fs = new FileStream (Path,filemode.open);
byte[] Imgsourse = new Byte[fs. Length];
Fs. Read (imgsourse,0,imgsourse.length);
Fs. Close ();
using (SqlConnection conn = new SqlConnection (SQLHELPER.CONNSTR))
{
Conn. Open ();
using (SqlCommand cmd = conn. CreateCommand ())
{
Cmd.commandtext = "Update t_employee set imagelogo= @ImageLogo";
Cmd. Parameters.Add ("@ImageLogo", sqldbtype.image);
Cmd. Parameters.Add (New SqlParameter ("@ImageLogo", Imgsourse));
return CMD. ExecuteNonQuery ();
}
}
}
Second, take picture data out of SQL Server and display it on the PictureBox control:
Copy Code code as follows:
byte[] Imagelogoarray = row["Imagelogo" is DBNull? Null:(byte[]) (row["Imagelogo"]);
MemoryStream Ms=null;
if (imagelogoarray!=null)
{
ms = new MemoryStream (Imagelogoarray);
Picbox.image = new Bitmap (MS);
}