上传图片并生成缩略图,返回上传图片的文件名
#region上传图片并生成缩略图,返回上传图片的文件名
/**////<summary>
///提取上传图片路径
///</summary>
///<paramname="fupImage"></param>
///<returns></returns>
privatestringgetFileName(System.Web.UI.HtmlControls.HtmlInputFilefupImage)
...{
上传正常尺寸的图片#region上传正常尺寸的图片
/**/////上传文件;
//stringpathName="NULL";
//stringnam=fupImage.PostedFile.FileName;
/**/////取得文件名(包括路径)里最后一个"."的索引
//inti=nam.LastIndexOf(".");
/**///取得文件扩展名
//stringnewext=nam.Substring(i).ToLower();
//Randomrnd=newRandom();
//stringnewname=System.DateTime.Now.Hour.ToString()+System.DateTime.Now.Second.ToString()+rnd.Next(1000,9999).ToString()+newext;
//stringthenam=Path.GetFileName(newname);
//stringfilestr=Server.MapPath("../images");
//fupImage.PostedFile.SaveAs(filestr+"/"+thenam);
#endregion
生成缩略图#region生成缩略图
stringpathName=string.Empty;
//生成原图
Byte[]oFileByte=newbyte[fupImage.PostedFile.ContentLength];
System.IO.StreamoStream=fupImage.PostedFile.InputStream;
System.Drawing.ImageoImage=System.Drawing.Image.FromStream(oStream);
intoWidth=oImage.Width;//原图宽度
intoHeight=oImage.Height;//原图高度
inttWidth=150;//设置缩略图初始宽度
inttHeight=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)));
}
//生成缩略原图
BitmaptImage=newBitmap(tWidth,tHeight);
Graphicsg=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,newRectangle(0,0,tWidth,tHeight),newRectangle(0,0,oWidth,oHeight),GraphicsUnit.Pixel);
stringtFullName=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(Exceptionex)
...{
throwex;
}
finally
...{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
#endregion
returnpathName;
}
#endregion
/**////<summary>
///提取上传图片路径
///</summary>
///<paramname="fupImage"></param>
///<returns></returns>
privatestringgetFileName(System.Web.UI.HtmlControls.HtmlInputFilefupImage)
...{
上传正常尺寸的图片#region上传正常尺寸的图片
/**/////上传文件;
//stringpathName="NULL";
//stringnam=fupImage.PostedFile.FileName;
/**/////取得文件名(包括路径)里最后一个"."的索引
//inti=nam.LastIndexOf(".");
/**///取得文件扩展名
//stringnewext=nam.Substring(i).ToLower();
//Randomrnd=newRandom();
//stringnewname=System.DateTime.Now.Hour.ToString()+System.DateTime.Now.Second.ToString()+rnd.Next(1000,9999).ToString()+newext;
//stringthenam=Path.GetFileName(newname);
//stringfilestr=Server.MapPath("../images");
//fupImage.PostedFile.SaveAs(filestr+"/"+thenam);
#endregion
生成缩略图#region生成缩略图
stringpathName=string.Empty;
//生成原图
Byte[]oFileByte=newbyte[fupImage.PostedFile.ContentLength];
System.IO.StreamoStream=fupImage.PostedFile.InputStream;
System.Drawing.ImageoImage=System.Drawing.Image.FromStream(oStream);
intoWidth=oImage.Width;//原图宽度
intoHeight=oImage.Height;//原图高度
inttWidth=150;//设置缩略图初始宽度
inttHeight=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)));
}
//生成缩略原图
BitmaptImage=newBitmap(tWidth,tHeight);
Graphicsg=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,newRectangle(0,0,tWidth,tHeight),newRectangle(0,0,oWidth,oHeight),GraphicsUnit.Pixel);
stringtFullName=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(Exceptionex)
...{
throwex;
}
finally
...{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
#endregion
returnpathName;
}
#endregion