ASP.NET实现上传图片并生成缩略图的方法
本文实例讲述了ASP.NET实现上传图片并生成缩略图的方法。分享给大家供大家参考,具体如下:
protectedvoidbt_upload_Click(objectsender,EventArgse)
{
//检查上传文件的格式是否有效
if(this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image")<0)
{
Response.Write("上传图片格式无效!");
return;
}
//生成原图
Byte[]oFileByte=newbyte[this.UploadFile.PostedFile.ContentLength];
System.IO.StreamoStream=this.UploadFile.PostedFile.InputStream;
System.Drawing.ImageoImage=System.Drawing.Image.FromStream(oStream);
intoWidth=oImage.Width;//原图宽度
intoHeight=oImage.Height;//原图高度
inttWidth=100;//设置缩略图初始宽度
inttHeight=100;//设置缩略图初始高度
//按比例计算出缩略图的宽度和高度
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;//设置高质量,低速度呈现平滑程度
g.Clear(Color.Transparent);//清空画布并以透明背景色填充
g.DrawImage(oImage,newRectangle(0,0,tWidth,tHeight),newRectangle(0,0,oWidth,oHeight),GraphicsUnit.Pixel);
stringoFullName=Server.MapPath(".")+"/image/"+"o"+DateTime.Now.ToShortDateString().Replace("-","")+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+".jpg";//保存原图的物理路径
stringtFullName=Server.MapPath(".")+"/image/"+"t"+DateTime.Now.ToShortDateString().Replace("-","")+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+".jpg";//保存缩略图的物理路径
try
{
//以JPG格式保存图片
oImage.Save(oFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
tImage.Save(tFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(Exceptionex)
{
throwex;
}
finally
{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
}
}
这里再补充一个改进方法:
#region上传图片并生成缩略图
///<summary>
///上传图片生成缩略图
///</summary>
///<paramname="originalImagePath">图片源路径</param>
///<paramname="thumbnailPath">缩略图路径(物理路径)</param>
///<paramname="width">缩略图宽度</param>
///<paramname="height">缩略图高度</param>
///<paramname="mode">生成缩略图的方式</param>
publicstaticvoidMakeThumbnail(stringoriginalImagePath,stringthumbnailPath,intwidth,intheight,stringmode){
//从路径中获取源图片的文件
System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(originalImagePath);
inttowidth=width;
inttoheight=height;
intx=0;
inty=0;
//获取图片的宽度
intow=image.Width;
//获取图片的高度
intoh=image.Height;
//生成缩略图的方式
switch(mode){
case"HW":
break;
case"W"://指定宽度高按比例
toheight=originalImage.Height*width/originalImage.Width;
break;
case"H"://指定图片的高度宽按比例
towidth=originalImage.Width*height/originalImage.Height;
break;
case"Cut"://如果为裁减模式则不变形
if((double)originalImage.Width/(double)originalImage.Height>(double)towidth/(double)toheight)
{
oh=originalImage.Height;
//缩略图片的宽度
ow=originalImage.Height*towidth/toheight;
y=0;
x=(originalImage.Width-ow)/2;
}
else{
ow=originalImage.Width;
//缩略图片的高度
oh=originalImage.Width*toheight/towidth;
x=0;
y(originalImage.Height-oh)/2;
}
break;
default:break;
}
//新建一个bmp图片
Bitmapbitmap=newBitmap(towidth,toheight);
//新建一个画布以BitMap宽高作为画布的大小
Graphicsg=Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode=InterpolationMode.High;
//以高质量低速度呈现
g.SmoothingMode=SmoothingMode.HighQuality;
//清空画布以白色背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage,newRectangle(towidth,toheight),newRectangle(x,y,ow,oh),GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(Exceptionex)
{
throwex;
}
finally{
//释放资源
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
#endregion
希望本文所述对大家asp.net程序设计有所帮助。