上传图片并生成缩略图,返回上传图片的文件名#region 上传图片并生成缩略图,返回上传图片的文件名
/**//// <summary>
/// 提取上传图片路径
/// </summary>
/// <param name="fupImage"></param>
/// <returns></returns>
private string getFileName(System.Web.UI.HtmlControls.HtmlInputFile fupImage)
...{
上传正常尺寸的图片#region 上传正常尺寸的图片
/**/////上传文件;
//string pathName = "NULL";
//string nam = fupImage.PostedFile.FileName;
/**/////取得文件名(包括路径)里最后一个"."的索引
//int i = nam.LastIndexOf(".");
/**///////取得文件扩展名
//string newext = nam.Substring(i).ToLower();
//Random rnd = new Random();
//string newname = System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Second.ToString() + rnd.Next(1000, 9999).ToString() + newext;
//string thenam = Path.GetFileName(newname);
//string filestr = Server.MapPath("../images");
//fupImage.PostedFile.SaveAs(filestr + "/" + thenam);
#endregion
生成缩略图#region 生成缩略图
string pathName = string.Empty;
//生成原图
Byte[] oFileByte = new byte[fupImage.PostedFile.ContentLength];
System.IO.Stream oStream = fupImage.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
int oWidth = oImage.Width; //原图宽度
int oHeight = oImage.Height; //原图高度
int tWidth = 150; //设置缩略图初始宽度
int tHeight = 200; //设置缩略图初始高度
//按比例计算出缩略图的宽度和高度
if (oWidth >= oHeight)
...{
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
}
else
...{
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
}
//生成缩略原图
Bitmap tImage = new Bitmap(tWidth, tHeight);
Graphics g = Graphics.FromImage(tImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 字串5
g.Clear(Color.Transparent); //清空画布并以透明背景色填充
g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
string tFullName = Server.MapPath("../images") + "/" + "t" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存缩略图的物理路径
pathName = "t" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg";
try
...{
//以JPG格式保存图片
tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
...{
throw ex;
}
finally
...{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
#endregion
return pathName;
}
#endregion
/**//// <summary>
/// 提取上传图片路径
/// </summary>
/// <param name="fupImage"></param>
/// <returns></returns>
private string getFileName(System.Web.UI.HtmlControls.HtmlInputFile fupImage)
...{
上传正常尺寸的图片#region 上传正常尺寸的图片
/**/////上传文件;
//string pathName = "NULL";
//string nam = fupImage.PostedFile.FileName;
/**/////取得文件名(包括路径)里最后一个"."的索引
//int i = nam.LastIndexOf(".");
/**///////取得文件扩展名
//string newext = nam.Substring(i).ToLower();
//Random rnd = new Random();
//string newname = System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Second.ToString() + rnd.Next(1000, 9999).ToString() + newext;
//string thenam = Path.GetFileName(newname);
//string filestr = Server.MapPath("../images");
//fupImage.PostedFile.SaveAs(filestr + "/" + thenam);
#endregion
生成缩略图#region 生成缩略图
string pathName = string.Empty;
//生成原图
Byte[] oFileByte = new byte[fupImage.PostedFile.ContentLength];
System.IO.Stream oStream = fupImage.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
int oWidth = oImage.Width; //原图宽度
int oHeight = oImage.Height; //原图高度
int tWidth = 150; //设置缩略图初始宽度
int tHeight = 200; //设置缩略图初始高度
//按比例计算出缩略图的宽度和高度
if (oWidth >= oHeight)
...{
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
}
else
...{
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
}
//生成缩略原图
Bitmap tImage = new Bitmap(tWidth, tHeight);
Graphics g = Graphics.FromImage(tImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 字串5
g.Clear(Color.Transparent); //清空画布并以透明背景色填充
g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
string tFullName = Server.MapPath("../images") + "/" + "t" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存缩略图的物理路径
pathName = "t" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg";
try
...{
//以JPG格式保存图片
tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
...{
throw ex;
}
finally
...{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
#endregion
return pathName;
}
#endregion