问题描述
个位大侠:我想在用户上传图片时将图片进行适当地裁减,比如原图片为800*600现在我要将它裁成200*150应当怎样实现呀?
解决方案
解决方案二:
参考:http://blog.csdn.net/ITFLY8/archive/2007/03/25/1540490.aspx
解决方案三:
该回复于2007-12-04 11:18:57被版主删除
解决方案四:
mark学习了,LZ如果找到了好方法请告诉我。
解决方案五:
http://www.cnblogs.com/adandelion/archive/2007/09/19/232858.html
解决方案六:
给个等比例缩放图片代码你参考参考Pagebasepbase=newPagebase();if(!pbase.ChkBadWord(context.Request.QueryString.ToString())&&context.Request["pic"]!=null&&context.Request["pic"].IndexOf(".")>0){stringpic=context.Request["pic"];intW=135;intH=110;intnewh=H;intneww=W;Bitmapbit=newBitmap(System.Configuration.ConfigurationManager.AppSettings["UpLoadFile"]+pic.Replace("/upload/","").Replace("/images/",""));MemoryStreamms=newMemoryStream();if(context.Request["W"]==null||context.Request["H"]==null||context.Request["W"]==""||context.Request["H"]==""){}else{try{W=int.Parse(context.Request["W"]);H=int.Parse(context.Request["H"]);}catch{context.Response.End();}if(pic==null||pic==""||W<=0||H<=0||pic.IndexOf(".")<1){context.Response.End();}//if(bit.Width<W&&bit.Height<H){newh=bit.Height;neww=bit.Width;}else{doublepwidth=(double)bit.Width/W;doublepheight=(double)bit.Height/H;if(pwidth>pheight){neww=W;newh=Convert.ToInt32(bit.Height/pwidth);}else{newh=H;neww=Convert.ToInt32(bit.Width/pheight);}}}//生成小图Bitmapnewbit=newBitmap(bit,neww,newh);newbit.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);context.Response.ContentType="image/jpeg";context.Response.StatusCode=200;context.Response.BinaryWrite(ms.ToArray());//context.Response.BinaryWrite(}else{context.Response.End();}
解决方案七:
BitmapBigBmp=newBitmap(800,600);BitmapSamllBmp=newBitmap(200,150);Graphicsg=Graphics.FromImage(SamllBmp);g.DrawImage(BigBmp,newRectangle(0,0,200,150));g.Dispose();
解决方案八:
缩略图吧这样可能效果就差了
解决方案九:
.NET的GDI+中有一个缩略图的类。网上很容易搜到代码,非常简单
解决方案十:
路过。
解决方案十一:
///<summary>///生成缩略图///</summary>///<paramname="originalImagePath">源图路径(物理路径)</param>///<paramname="thumbnailPath">缩略图路径(物理路径)</param>///<paramname="width">缩略图宽度</param>///<paramname="height">缩略图高度</param>///<paramname="mode">生成缩略图的方式,HW,W,H,Cut</param>publicvoidMakeThumbnail(stringoriginalImagePath,stringthumbnailPath,intwidth,intheight,stringmode){System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(originalImagePath);inttowidth=width;inttoheight=height;intx=0;inty=0;intow=originalImage.Width;intoh=originalImage.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*height/towidth;x=0;y=(originalImage.Height-oh)/2;}break;default:break;}//新建一个bmp图片System.Drawing.Imagebitmap=newSystem.Drawing.Bitmap(towidth,toheight);//新建一个画板System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(bitmap);//设置高质量插值法g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量,低速度呈现平滑程度g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;//清空画布并以透明背景色填充g.Clear(System.Drawing.Color.Transparent);//在指定位置并且按指定大小绘制原图片的指定部分g.DrawImage(originalImage,newSystem.Drawing.Rectangle(0,0,towidth,toheight),newSystem.Drawing.Rectangle(x,y,ow,oh),System.Drawing.GraphicsUnit.Pixel);try{//以jpg格式保存缩略图bitmap.Save(thumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);}catch(System.Exceptione){throwe;}finally{originalImage.Dispose();bitmap.Dispose();g.Dispose();}}