02-20 WinForm upload pictures and read pictures

Source: Internet
Author: User

Create a Windows Forms application, drag two buttons and a PictureBox in the Form1 interface to upload pictures and display pictures by entering an output stream. Need to add openFileDialog1.

The interface is as follows:

How to write upload and display pictures in CS

1  //Upload Image2         Private voidButton1_Click (Objectsender, EventArgs e)3         {4             //Conversion of Images5Openfiledialog1.filter ="*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";//set picture to save as file type format, filter is file filters6DialogResult da =Openfiledialog1.showdialog ();7             if(da==DialogResult.OK)8             {9                 stringFil =Openfiledialog1.filename;TenFileStream fs =NewFileStream (fil,filemode.open,fileaccess.read); One                 byte[] img=New byte[FS. Length]; ABinaryReader br =NewBinaryReader (FS);//Binary Reader -IMG =Br. Readbytes (Convert.ToInt32 (fs. Length)); -                 //Link Database theSqlConnection conn =NewSqlConnection ("server=.; Database=newdata;user=sa;pwd=123"); - Conn. Open (); -SqlCommand cmd =Conn. CreateCommand (); -Cmd.commandtext ="INSERT into table_1 values (@image)"; + cmd. Parameters.clear (); -Cmd. Parameters.Add ("@image", IMG); + cmd. ExecuteNonQuery (); A Conn. Close (); atMessageBox.Show ("Upload Successful"); -  -}
How to upload a picture
1   //reading pictures from the database2         Private voidButton2_Click (Objectsender, EventArgs e)3         {4SqlConnection conn =NewSqlConnection ("server=.; Database=newdata;user=sa;pwd=123");5 Conn. Open ();6SqlCommand cmd =Conn. CreateCommand ();7Cmd.commandtext ="Select top 1*from table_1";8SqlDataReader dr =cmd. ExecuteReader ();9 Dr. Read ();Ten             byte[] img = (byte[]) dr["Ino"]; One            //byte[] img1= (byte[]) dr["Ino"]; AMemoryStream ms =NewMemoryStream (IMG,0, IMG. Length); -Ms. Write (IMG,0, IMG. Length); -Image image =Image.fromstream (MS); the              This. Picturebox1.sizemode =Pictureboxsizemode.zoom; -              This. pictureBox1.Image =image; -}
ways to display pictures from a database

02-20 WinForm upload pictures and read 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.