Convert a picture to binary--convert binary to Picture

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.