usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Data.SqlClient;namespacewindowsformsapplication10{ Public Partial classForm2:form { PublicForm2 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) {Openfiledialog1.filter="@. jpg|*.jpg|@. gif|*.gif|@. Png|*.png| All Files|*.*"; DialogResult Dr=Openfiledialog1.showdialog (); if(dr = =DialogResult.OK) {//to read a picture into a file streamFileStream fs =NewFileStream (Openfiledialog1.filename,filemode.open,fileaccess.read);//PathImage img = System.Drawing.Bitmap.FromStream (FS);//draw, flow into picturespictureBox1.Image = img;//Specify, display pictureFS. Close (); } } Private voidButton2_Click (Objectsender, EventArgs e)//Deposit Database{Openfiledialog1.filter="@. jpg|*.jpg|@. gif|*.gif|@. Png|*.png| All Files|*.*"; DialogResult Dr=Openfiledialog1.showdialog (); if(dr = =DialogResult.OK) {//file StreamFileStream fs =NewFileStream (Openfiledialog1.filename,filemode.open,fileaccess.read); BinaryReader BR=NewBinaryReader (FS);//Binary Reader byte[] buffer = br. Readbytes (int. Parse (fs. Length.tostring ())); //connecting to a databaseSqlConnection conn =NewSqlConnection ("server=.; Database=snewdata;user=sa;pwd="); SqlCommand cmd=Conn. CreateCommand (); Cmd.commandtext="INSERT into imgtable values (@buffer)"; Cmd. Parameters.Add ("@buffer", buffer); Conn. Open (); Cmd. ExecuteNonQuery (); Cmd. Dispose (); Conn. Close (); } } Private voidButton3_Click (Objectsender, EventArgs e)//Database Read { //Read Database byte[] buffer =NULL; SqlConnection Conn=NewSqlConnection ("server=.; Database=snewdata;user=sa;pwd="); SqlCommand cmd=Conn. CreateCommand (); Cmd.commandtext="Select *from imgtable where code=4"; Conn. Open (); SqlDataReader Dr=cmd. ExecuteReader (); if(Dr. Read ()) {buffer= (byte[]) dr["IMGs"]; } cmd. Dispose (); Conn. Close (); //display the binary data buffer as a pictureMemoryStream ms =NewMemoryStream (buffer);//Building ObjectsMs. Write (Buffer,0, buffer. Length);//write to the memory streamImage img =System.Drawing.Image.FromStream (MS); pictureBox1.Image=img; } }}
WinForm picture read stored in database SQL