C#_ Image Access Database WinForm

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.