码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-02-04 18:27:01      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

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

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

标签:

原文地址:http://www.cnblogs.com/zhangruisoldier/p/4272799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!