Bind the image to pictureBox and save the image to the database for read and write.

Source: Internet
Author: User
Form2 browsing: stringfileNamestring. empty; privatevoidbutton1_Click (objectsender, EventArgse) {OpenFileDialogopenFileDialog1newOpenFileDialog (); openFileDialog1.Filter *. BMP ,*. JPG ;*. GIF | *. BMP ;*. JPG ;*. GIF; openFileDialog1.Sh

Form2 browsing: string fileName = string. empty; private void button1_Click (object sender, EventArgs e) {OpenFileDialog openFileDialog1 = new OpenFileDialog (); openFileDialog1.Filter = "*. BMP ,*. JPG ;*. GIF | *. BMP ;*. JPG ;*. GIF "; openFileDialog1.Sh

Form2

Browsing:

String fileName = string. Empty;

Private void button#click (object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog ();
OpenFileDialog1.Filter = "*. BMP, *. JPG; *. GIF | *. BMP; *. JPG; *. GIF ";
OpenFileDialog1.ShowDialog ();
FileName = openFileDialog1.FileName;
Image a = Image. FromFile (openFileDialog1.FileName );

Bitmap bit = new Bitmap (pictureBox1.Width, pictureBox1.Height );
Graphics g = Graphics. FromImage (bit); // create a new Graphics (drawing) from the specified Image ).
G. drawImage (a, new Rectangle (0, 0, bit. width, bit. height), new Rectangle (0, 0,. width,. height), GraphicsUnit. pixel );
// The first parameter: The image to be drawn
// The second parameter: it specifies the position and size of the image to be drawn. Scale the image to fit the rectangle.
// The third parameter: Specifies the part to be drawn in the image object.
PictureBox1.Image = bit;
}
OK: Upload to database
Private void button2_Click (object sender, EventArgs e)
{
FileStream fs = File. OpenRead (fileName );
Byte [] img = new byte [fs. Length];
Fs. Read (img, 0, img. Length );
Fs. Close ();
SqlConnection conn = new SqlConnection (ConfigurationSettings. etettings ["Conn"]. ToString ());
SqlParameter p = new SqlParameter ("@ images", img );
SqlCommand SC = new SqlCommand ("update Employees set employeeTx = @ images where employeeID = 2", conn );
SC. Parameters. Add (p );
If (SC. Connection. State = ConnectionState. Closed)
{
SC. Connection. Open ();
}

SC. ExecuteNonQuery ();
SC. Connection. Close ();
Form3 f = new Form3 ();
This. Hide ();
F. Show ();

}

The image is displayed in the Form3 form:

Private void Form3_Load (object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection (ConfigurationSettings. etettings ["Conn"]);
SqlCommand com = new SqlCommand ("select employeeTx from Employees where employeeID = 2", conn );
Conn. Open ();
Byte [] B = (byte []) com. ExecuteScalar ();
If (B. Length> 0)
{
MemoryStream s = new MemoryStream (B, true );
S. Write (B, 0, B. Length );

Image a = new Bitmap (s );

Bitmap bit = new Bitmap (pictureBox1.Width, pictureBox1.Height );
Graphics g = Graphics. FromImage (bit); // create a new Graphics (drawing) from the specified Image ).
G. drawImage (a, new Rectangle (0, 0, bit. width, bit. height), new Rectangle (0, 0,. width,. height), GraphicsUnit. pixel );
// The first parameter: The image to be drawn
// The second parameter: it specifies the position and size of the image to be drawn. Scale the image to fit the rectangle.
// The third parameter: Specifies the part to be drawn in the image object.
PictureBox1.Image = bit;
}
}

Also: draw borders

Private void picturebox#paint (object sender, PaintEventArgs e)
{
E. graphics. drawRectangle (new Pen (Color. black, 2), new Rectangle (new Point (1, 1), new Size (this. pictureBox1.Width-2, this. pictureBox1.Height-2); // The line width is 4
}

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.