Convert a picture to binary--convert binary to picture private void Button1_Click (object sender, EventArgs e) {string path = This.textbox 1.Text; byte[] Imgbytesin = saveimage (path); Showimgbybyte (Imgbytesin); Parameters.Add ("@Photo", sqldbtype.binary). Value = Imgbytesin; }//Put the picture in binary stream public byte[] SaveImage (String path) {FileStream fs = new Filest Ream (Path, FileMode.Open, FileAccess.Read); Save the picture as a file stream binaryreader br = new BinaryReader (FS); byte[] Imgbytesin = br. Readbytes ((int) fs. Length); Reads the stream into the byte array return imgbytesin; }//Real binary stream represents picture public void Showimgbybyte (byte[] imgbytesin) {MemoryStream ms = new Memor Ystream (Imgbytesin); pictureBox1.Image = Image.fromstream (ms); Save the picture to the database and read it from the database: #region read the picture from the database///<summary>///Read the picture from the database///</summary> <param NAme= "xs_id" > to read the number of pictures </param>//<param name= "ph" >picturebox1 control name </param> public void GE T_photo (string xs_id, PictureBox ph)//reads the picture from the database {byte[] imagebytes = null; Getcon (); SqlCommand con = new SqlCommand ("SELECT * from S_jiben where s_num= '" + xs_id + "'", link); SqlDataReader Dr = Con. ExecuteReader (); while (Dr. Read ()) {imagebytes = (byte[]) Dr. GetValue (18); } Dr. Close (); Con_close (); MemoryStream ms = new MemoryStream (imagebytes); Bitmap bmpt = new Bitmap (MS); Ph. Image = bmpt; } #endregion #region public void SaveImage (string MID, OpenFileDialog openf)//To store the picture in a binary database { String strimg = OpenF.FileName.ToString (); The path where the picture is recorded FileStream fs = new FileStream (strimg, FileMode.Open, FileAccess.Read); Save the picture as a file stream binaryreader br =New BinaryReader (FS); byte[] Imgbytesin = br. Readbytes ((int) fs. Length); Reads the stream into the byte array Getcon (); StringBuilder strSQL = new StringBuilder (); Strsql.append ("Update S_jiben Set [email protected] where s_num=" + MID); SqlCommand cmd = new SqlCommand (strsql.tostring (), link); Cmd. Parameters.Add ("@Photo", sqldbtype.binary). Value = Imgbytesin; Cmd. ExecuteNonQuery (); Con_close (); } #endregion
Convert a picture to binary--convert binary to Picture