Method 1:
The database uses SQL 2008, and the data table stores binary data of the picture. Now the picture is exported in a picture format (such as .jpg) and then stored in the specified folder. The implementation method is as follows:
byte[] bytImg = (byte[])("SELECT F_Photo FROM myTable WHERE ID=1").Tables[0].Rows[0][0]; if (bytImg != null) { MemoryStream ms = new MemoryStream(bytImg); Image img = (ms); ("D:\\"); }
Method 2:
It is a windowform program. The database has been built and the image is stored in the image table of the database in binary form. I want to read out the image (a large number of) that meets the query conditions from the database, display it in a control on the form form (listview or imagelist or picturebox? I don't know which one is suitable), and save it to a folder selected (or newly created)
SqlDataAdapter da = new SqlDataAdapter("select * from newpicture", conn);//Domain connection, modify the database operation.DataSet ds = new DataSet(); (ds, "pic");//Save the options that meet the conditions in the pic table of the dataset string picdotname; string picfilename; int piclength; int i; //Add a new columnDataColumn newcolumn = ["pic"].("pic_url", typeof(string));//Add a new column of pic_url to the pic table and save your newly written image pathfor (i = 0; i < Convert.ToInt16(["pic"].); i++) { picdotname = ["pic"].Rows[i]["pic_dot"].ToString();//The extension name of the image, your database must have this column, such as jpg piclength = Convert.ToInt32(["pic"].Rows[i]["pic_length"]);//Length of data stream picfilename = ("New folder name/") + "Add image name"+ "." + picdotname; FileStream fs = new FileStream(picfilename, , ); byte[] piccontent = new byte[piclength]; piccontent = (byte[])["pic"].Rows[i]["pic_content"]; (piccontent, 0, piclength); ();//Read the data stream and write it into a picture //Finally bind the table to the control. ["pic"].Rows[i]["pic_url"] = "temp/temp" + () + "." + picdotname;//It means to add the path value of the file to the i-th row of the table pic and the pic_url column.} //Data Source = ["pic"];//Data binding
This is generally the case. You can modify the list of many details in the table.
The above is the detailed content of two methods of C# reading and saving pictures from the database. For more information about C# reading and saving pictures, please pay attention to my other related articles!