Winform Accessing pictures

Source: Internet
Author: User

namespace windowsformsapplication10{public partial class Form2:form {public Form2 () {I        Nitializecomponent (); The private void Button1_Click (object sender, EventArgs e) {openfiledialog1.filter = "@. jpg|*.jpg|@. gif|*.gif|@. Png|*.png|            All Files|*.* ";            DialogResult dr = Openfiledialog1.showdialog (); if (dr = = DialogResult.OK) {//Read the picture into the file stream FileStream fs = new FileStream (openfiled Ialog1.                Filename,filemode.open,fileaccess.read);                          Image img = System.Drawing.Bitmap.FromStream (fs);//Draw pictureBox1.Image = img;//specified }} private void Button2_Click (object sender, EventArgs e) {openfiledialog1.filter = "@". jpg|*.jpg|@. gif|*.gif|@. Png|*.png|            All Files|*.* ";            DialogResult dr = Openfiledialog1.showdialog ();        if (dr = = DialogResult.OK) {//file stream        FileStream fs = new FileStream (openfiledialog1.filename,filemode.open,fileaccess.read); BinaryReader br = new BinaryReader (fs);//binary reader byte[] buffer = br. Readbytes (int. Parse (fs.                Length.tostring ())); Connect database SqlConnection conn = new SqlConnection ("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 void Button3_Click (object sender, EventArgs e) {//Read database SqlConnect Ion conn = new SqlConnection ("server=.;            Database=snewdata;user=sa;pwd= "); SqlCommand cmd = conn.            CreateCommand ();            Cmd.commandtext = "Select *from imgtable where code=4"; CoNn.            Open (); SqlDataReader dr = cmd.            ExecuteReader (); Dr.            Read ();                        byte[] buffer = (byte[]) dr["IMGs"]; Cmd.            Dispose (); Conn.            Close (); Display the binary data buffer as a picture MemoryStream ms = new MemoryStream (buffer);//Build Object Ms. Write (Buffer,0,buffer.            LENGTH);//write to the memory stream in Image img = System.Drawing.Image.FromStream (ms);        pictureBox1.Image = img; }    }}

  

Winform Accessing pictures

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.