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
}