Asp. NET stores pictures in a binary way into the database and reads

Source: Internet
Author: User

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

Related Article

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.