PictureBox upload pictures from local and save in disk directory

Source: Internet
Author: User

private void Mypicbox_click (object sender, EventArgs e)
{
Try
{
OpenFileDialog ofdpic = new OpenFileDialog ();
Ofdpic.filter = "*.jpg,*.jpeg,*.bmp,*.gif,*.ico,*.png,*.tif,*.wmf|*.jpg;*.jpeg;*.bmp;*.gif;*.ico;*.png;*.tif;*. WMF ";
Ofdpic.filterindex = 1;
Ofdpic.filename = "";
if (ofdpic.showdialog () = = DialogResult.OK)
{
String spicpaht = OfdPic.FileName.ToString ();
Bitmap bmpic = new Bitmap (SPICPAHT);
Point ptloction = new Point (bmpic.size);
if (Ptloction.x > Mypicbox. Size.width | | PTLOCTION.Y > Mypicbox. Size.Height)
{
Image fills the image frame, and the image is held in proportion
Mypicbox. SizeMode = Pictureboxsizemode.zoom;
}
Else
{
The image is placed in the image frame
Mypicbox. SizeMode = Pictureboxsizemode.centerimage;
}
Gets the path of the image
Mypicbox. Load (SPICPAHT);
Mypicbox. Image = This.mypicbox.Image;
AutoSave (Mypicbox);
}
}
catch (Exception exp)
{
Throw exp;
}
}
private void AutoSave (PictureBox mypicbox)
{
String Opath = @ "Data\image";
String photoname = DateTime.Now.Ticks.ToString ();
This.imagename = Photoname;
if (Opath.substring (opath.length-1, 1)! = @ "\")
Opath = Opath + @ "\";
String path1 = Opath + DateTime.Now.ToShortDateString ();
if (! Directory.Exists (path1))
{
Directory.CreateDirectory (path1);
}
PictureBox1.Image.Save (path1 + "\" + Photoname + ". jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
Shrinking the image
System.Drawing.Bitmap Objpic, Objnewpic;
Try
{
Mypicbox. Image.Save (path1);
Objpic = new System.Drawing.Bitmap (Mypicbox. Image);
Objnewpic = new System.Drawing.Bitmap (Objpic, Mypicbox. Width, Mypicbox. Height);
Objnewpic=new System.Drawing.Bitmap (objpic,320,240);//Picture saved size size
Objnewpic.save (path1 + "\" + ImageName + ". jpg");
}
catch (Exception exp) {throw exp;}
Finally
{
Objpic = null;
Objnewpic = null;
}
}
private string ImageName {get; set;}

PictureBox upload pictures from local and save in disk directory

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.