Winfrom image Transfer

Source: Internet
Author: User

private void Button1_Click (object sender, EventArgs e)//Open picture from hard disk displayed inside the form        {           OpenFileDialog op = new OpenFileDialog ();//Create an Open dialog box           DialogResult Dr =op. ShowDialog ()///Opens the dialog box to show if            (Dr==dialogresult.ok)//If the point is "open"            {                FileStream fs = new FileStream (op. Filename,filemode.open); Get the path of your open picture to the FS stream inside to                //messagebox.show (op. FileName);                Image img = System.Drawing.Bitmap.FromStream (fs);//Draw fs by drawing function                pictureBox1.Image = img;//picture contents            }        }

 //Here's a picture of opening a photo to the database.        Private voidButton2_Click (Objectsender, EventArgs e)//open a picture and save it in the database.{OpenFileDialog op=NewOpenFileDialog (); DialogResult Dr=op.            ShowDialog (); if(dr = =DialogResult.OK) {FileStream fs=NewFileStream (Op. Filename,filemode.open);//get the path to your open picture and save it inside FS//The following begins to convert the path to bytes into the database.BinaryReader br =NewBinaryReader (FS);//Convert FS (stream that holds picture path) to binary deposit into BR inside go binaryreader binary reader//int[] x= new int[10];                byte[] butter=New byte[FS. Length];//create an array of byte typesButter = br. Readbytes ((int) fs. Length);//Assigns a value to the byte butter, which is the stream converted to binary data//start connecting to the database below and then save the picture inside.SqlConnection conn =NewSqlConnection ("server=.; Database=cml;user=sa;pwd=123"); SqlCommand cmd=Conn.                CreateCommand (); Conn.                Open (); Cmd.commandtext="insert into LK values (@s)"; Cmd.                Parameters.clear (); Cmd. Parameters.Add ("@s", butter);//butter Open a picture's path → binary data → bytescmd.                ExecuteNonQuery (); Cmd.                Dispose (); Conn.            Close (); }        }



private void Button3_Click (object sender, EventArgs e)//from the database, the picture is displayed on the form        {            SqlConnection conn = new SqlConnection ("server=.; Database=cml;user=sa;pwd=123 ");            SqlCommand cmd = conn. CreateCommand ();            Conn. Open ();            Cmd.commandtext = "Select Top 1* from the LK Order by IDs DESC"; Order by sort GROUP by group            SqlDataReader dr = cmd. ExecuteReader ();            Dr. Read ();//start reading data                        byte[] butter = (byte[]) dr["IMG"];              Butter is now a byte, but the image type inside the form is image, so to go back to   MemoryStream is the memory stream            MemoryStream ms = new MemoryStream (butter);// The butter has been converted to stream            image img = System.Drawing.Bitmap.FromStream (ms);//to draw the stream into a picture            pictureBox1.Image = img;        }

Winfrom image Transfer

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.