Convert a picture to binary--convert binary to Picture
Privatevoid Button1_Click (Objectsender, EventArgs e) {String path =This. TextBox1.Text;byte[] Imgbytesin =SaveImage (path); Showimgbybyte (Imgbytesin);//Parameters.Add ("@Photo", sqldbtype.binary). Value = Imgbytesin;}//To add a picture to a binary streamPublicByte[] saveimage (String path) {FileStream fs =New FileStream (Path, FileMode.Open, FileAccess.Read);// Save the picture as a file stream binaryreader br = new BinaryReader (FS); byte[] Imgbytesin = br. Readbytes ((int) fs. Length); // read stream into byte array return< Span style= "color: #000000;" > Imgbytesin; } // Real binary stream represents the picture public void showimgbybyte (byte [] imgbytesin) {MemoryStream ms = new MemoryStream (Imgbytesin); pictureBox1.Image = Image.fromstream (ms);}
Second, save the picture to the database and read from the database:
#region to read pictures from the database///<summary>///To read a picture from the database///</summary>///<param name= "xs_id" >To read the number of the picture</param>///<param name= "PH" >PictureBox1 Control Name</param>Publicvoid Get_photo (String xs_id, PictureBox ph)//To read a 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 =NewMemoryStream (imagebytes); Bitmap bmpt =NewBitmap (MS); Ph. Image =Bmpt; }#endregion#regionPublicvoid SaveImage (String MID, OpenFileDialog openf)//To store a 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 =NewBinaryReader (FS);byte[] Imgbytesin = br. Readbytes ((int) fs. Length);// read stream into 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 Imgbytesin; cmd. ExecuteNonQuery (); Con_close (); } #endregion
Travel Network www.jieberu.com
Push-push family, free ticket acquisition platform Www.tuituizu.com
ASP. NET stores the image in a binary way and reads the