PictureBox从本地上传图片和保存在磁盘目录

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; }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。