try
{
//得到图片的名称
string name = System.IO.Path.GetFileNameWithoutExtension(imgSrc);
//得到图片的后缀名
string extName = System.IO.Path.GetExtension(imgSrc);
string Name = System.IO.Path.GetFileName(imgSrc);
string s = Server.MapPath("/" + Name);
//生成一个新的名称
string newName = Guid.NewGuid() + extName;
//得到图片文件夹所在的物理路径
string oldRelative = "/Content/Images/HeadIMG/old/";
string oldPath = Server.MapPath(oldRelative);
string thumRelative = "/Content/Images/HeadIMG/thum/";
string thumPath = Server.MapPath(thumRelative);
FileStream fs = null;
fs = new FileStream(imgSrc, FileMode.Open);
using (System.Drawing.Image img = System.Drawing.Image.FromStream(fs))
{
//保存原图
img.Save(oldPath + newName);
//得到缩略图
System.Drawing.Image thumImg = img.GetThumbnailImage(20, 20, null, System.IntPtr.Zero);
thumImg.Save(thumPath + newName);
}
//储存压缩图片路径
HttpCookie imgcook = new HttpCookie("ImgThumPath", thumRelative + newName);
imgcook.Expires = DateTime.Now.AddDays(3);
Response.Cookies.Add(imgcook);
return Json(0, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(1, JsonRequestBehavior.AllowGet);
throw;
}