private void Button1_Click (object sender, EventArgs e)//Open picture from hard disk displayed inside the form { OpenFileDialog op = new OpenFileDialog ();//Create an Open dialog box DialogResult Dr =op. ShowDialog ()///Opens the dialog box to show if (Dr==dialogresult.ok)//If the point is "open" { FileStream fs = new FileStream (op. Filename,filemode.open); Get the path of your open picture to the FS stream inside to //messagebox.show (op. FileName); Image img = System.Drawing.Bitmap.FromStream (fs);//Draw fs by drawing function pictureBox1.Image = img;//picture contents } }
//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 void Button3_Click (object sender, EventArgs e)//from the database, the picture is displayed on the form { SqlConnection conn = new SqlConnection ("server=.; Database=cml;user=sa;pwd=123 "); SqlCommand cmd = conn. CreateCommand (); Conn. Open (); Cmd.commandtext = "Select Top 1* from the LK Order by IDs DESC"; Order by sort GROUP by group SqlDataReader 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 to MemoryStream is the memory stream MemoryStream ms = new MemoryStream (butter);// The butter has been converted to stream image img = System.Drawing.Bitmap.FromStream (ms);//to draw the stream into a picture pictureBox1.Image = img; }
Winfrom image Transfer