#region to display the selected picture in the PictureBox control
<summary>
Used to display the selected picture in the PictureBox control
</summary>
<param name= "Openf" > Image name </param>
<param name= "MyImage" >picturebox control id</param>
public void Read_image (OpenFileDialog openf, PictureBox myimage)//Display selected picture
{
Specifies the file format that the OpenFileDialog control opens
Openf.filter = "*.jpg|*.jpg|*.bmp|*.bmp";
if (Openf.showdialog () ==dialogresult.ok)
{
Try
{
To save a picture file in a PictureBox control
Myimage.image = System.Drawing.Image.FromFile (openf.filename);
}
catch (Exception)
{
Pop-up error message
MessageBox.Show ("The picture you selected cannot be read or the file type is not correct!") "," error ", messageboxbuttons.ok,messageboxicon.warning);
Throw
}
}
}
#endregion
#region used to store pictures in the database in binary form
<summary>
Used to store pictures in a binary form in the database
</summary>
<param name= "Filmid" > Videos id</param>
<param name= "Openf" ></param>
public void SaveImage (string filmid, OpenFileDialog openf)//The picture is stored in a binary database
{
String strimg = OpenF.FileName.ToString (); Record the path where the picture is located
FileStream fs = new FileStream (strimg, FileMode.Open, FileAccess.Read); Save a picture as a file stream
BinaryReader br = new BinaryReader (FS);
byte[] Imgbytesin = br. Readbytes ((int) fs. LENGTH);//stream is read into the byte array
SqlConnection conn = Sqlhelper.getcon ();
Conn. Open ();
StringBuilder strSQL = new StringBuilder ();
Strsql.append ("Update T_film Set[email protected]where f_fid= "+ filmid);
SqlCommand cmd = new SqlCommand (strsql.tostring (), conn);
Cmd. Parameters.Add ("@Photo", sqldbtype.binary). Value = Imgbytesin;
Cmd. ExecuteNonQuery ();
Conn. Close ();
}
#endregion
#region to take pictures out of the database and display them in the PictureBox control
<summary>
Used to take pictures out of the database and display them in the PictureBox control
</summary>
<param name= "Filmid" > Videos id</param>
<param name= "PB" >picturebox control id</param>
public void Get_image (string filmid, PictureBox pb)//Take pictures out of the database
{
byte[] imagebytes = null;
SqlConnection conn = Sqlhelper.getcon ();
Conn. Open ();
SqlCommand com = new SqlCommand ("SELECT * from T_film where f_fid= '" + filmid + "'", conn);
SqlDataReader dr = com. ExecuteReader ();
while (Dr. Read ())
{
Imagebytes = (byte[]) Dr. GetValue (10);
}
Dr. Close ();
Conn. Close ();
MemoryStream ms = new MemoryStream (imagebytes);
Bitmap bmpt = new Bitmap (MS);
Pb. Image = bmpt;
}
#endregion
The above three are public classes, the first one is the open dialog, the second is the binary to the database, and the third one is read from the database.
In the form code, Mymenu.get_image (Filmid, Picboxphoto); direct use (Mymenu) is a public class space name
C#_ Image Access Database WinForm