Private voidButton1_Click (Objectsender, EventArgs e)//open pictures from hard disk appear inside the form{OpenFileDialog op=NewOpenFileDialog ();//Create an open dialog boxDialogResult Dr =op. ShowDialog ();//The Open dialog box is displayed. if(Dr==dialogresult.ok)//If the point is "open"{FileStream fs=NewFileStream (Op. Filename,filemode.open);//get the path to your open picture and save it inside the FS stream.//MessageBox.Show (Op. FileName);Image img = System.Drawing.Bitmap.FromStream (FS);//draw fs out through the drawing functionpictureBox1.Image = img;//Picture contents of picture frame } }
///////////////
//Here's a picture of opening a photo to the database. Private voidButton2_Click (Objectsender, EventArgs e)//open a picture and save it in the database.{OpenFileDialog op=NewOpenFileDialog (); DialogResult Dr=op. ShowDialog (); if(dr = =DialogResult.OK) {FileStream fs=NewFileStream (Op. Filename,filemode.open);//get the path to your open picture and save it inside FS//The following begins to convert the path to bytes into the database.BinaryReader br =NewBinaryReader (FS);//Convert FS (stream that holds picture path) to binary deposit into BR inside go binaryreader binary reader//int[] x= new int[10]; byte[] butter=New byte[FS. Length];//create an array of byte typesButter = br. Readbytes ((int) fs. Length);//Assigns a value to the byte butter, which is the stream converted to binary data//start connecting to the database below and then save the picture inside.SqlConnection conn =NewSqlConnection ("server=.; Database=cml;user=sa;pwd=123"); SqlCommand cmd=Conn. CreateCommand (); Conn. Open (); Cmd.commandtext="insert into LK values (@s)"; Cmd. Parameters.clear (); Cmd. Parameters.Add ("@s", butter);//butter Open a picture's path → binary data → bytescmd. ExecuteNonQuery (); Cmd. Dispose (); Conn. Close (); } }
/////////////////////
Private voidButton3_Click (Objectsender, EventArgs e)//to display a picture on a form from a database{SqlConnection conn=NewSqlConnection ("server=.; Database=cml;user=sa;pwd=123"); SqlCommand cmd=Conn. CreateCommand (); Conn. Open (); Cmd.commandtext="select top 1* from LK order by IDs Desc";//ORDER by sort GROUP by groupSqlDataReader dr =cmd. ExecuteReader (); Dr. Read ();//Start reading Data byte[] butter = (byte[]) dr["img"]; //butter is now a byte, but the image type inside the form is image, so to go back MemoryStream is the memory streamMemoryStream ms =NewMemoryStream (butter);//has converted the butter into a stream.Image img = System.Drawing.Bitmap.FromStream (ms);//draw a stream into a picturepictureBox1.Image =img; }
Transfer pictures between databases and forms