Create a Windows Forms application, drag two buttons and a PictureBox in the Form1 interface to upload pictures and display pictures by entering an output stream. Need to add openFileDialog1.
The interface is as follows:
How to write upload and display pictures in CS
1 //Upload Image2 Private voidButton1_Click (Objectsender, EventArgs e)3 {4 //Conversion of Images5Openfiledialog1.filter ="*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";//set picture to save as file type format, filter is file filters6DialogResult da =Openfiledialog1.showdialog ();7 if(da==DialogResult.OK)8 {9 stringFil =Openfiledialog1.filename;TenFileStream fs =NewFileStream (fil,filemode.open,fileaccess.read); One byte[] img=New byte[FS. Length]; ABinaryReader br =NewBinaryReader (FS);//Binary Reader -IMG =Br. Readbytes (Convert.ToInt32 (fs. Length)); - //Link Database theSqlConnection conn =NewSqlConnection ("server=.; Database=newdata;user=sa;pwd=123"); - Conn. Open (); -SqlCommand cmd =Conn. CreateCommand (); -Cmd.commandtext ="INSERT into table_1 values (@image)"; + cmd. Parameters.clear (); -Cmd. Parameters.Add ("@image", IMG); + cmd. ExecuteNonQuery (); A Conn. Close (); atMessageBox.Show ("Upload Successful"); - -}
How to upload a picture
1 //reading pictures from the database2 Private voidButton2_Click (Objectsender, EventArgs e)3 {4SqlConnection conn =NewSqlConnection ("server=.; Database=newdata;user=sa;pwd=123");5 Conn. Open ();6SqlCommand cmd =Conn. CreateCommand ();7Cmd.commandtext ="Select top 1*from table_1";8SqlDataReader dr =cmd. ExecuteReader ();9 Dr. Read ();Ten byte[] img = (byte[]) dr["Ino"]; One //byte[] img1= (byte[]) dr["Ino"]; AMemoryStream ms =NewMemoryStream (IMG,0, IMG. Length); -Ms. Write (IMG,0, IMG. Length); -Image image =Image.fromstream (MS); the This. Picturebox1.sizemode =Pictureboxsizemode.zoom; - This. pictureBox1.Image =image; -}
ways to display pictures from a database
02-20 WinForm upload pictures and read pictures