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)
{
//图像充滿图像框,並且图像維持比例
mypicbox.SizeMode = PictureBoxSizeMode.Zoom;
}
else
{
//图像在图像框置中
mypicbox.SizeMode = PictureBoxSizeMode.CenterImage;
}
//获取图像的Path
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);
//图像的缩小
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);//图片保存的大小尺寸
objNewPic.Save(path1 + "\\" + imageName + ".jpg");
}
catch (Exception exp) { throw exp; }
finally
{
objPic = null;
objNewPic = null;
}
}
private string imageName { get; set; }