问题描述
从用.NetWeb开发到现在所看到的略缩生成代码都不尽人意,要不太局限,要不失真厉害。为此写了一个相对完善的函数供大家学习。其中的SaveIamge函数提供了失真解决方法,对于处理过的图片(如加水印……)要求保持高品质可以直接调用。usingSystem;usingSystem.IO;usingSystem.Drawing;usingSystem.Drawing.Drawing2D;usingSystem.Drawing.Imaging;usingSystem.Drawing.Text;usingSystem.Text.RegularExpressions;///<summary>//图片处理。//http://www.hulaka.com///(c)2008Dao///</summary>publicclassImage{///<summary>///缩略模式。///</summary>publicenumThumbMode:byte{///<summary>///完整模式///</summary>Full=1,///<summary>///最大尺寸///</summary>Max}///<summary>///缩略图。///</summary>///<paramname="image">要缩略的图片</param>///<paramname="size">要缩放的尺寸</param>///<paramname="mode">缩略模式</param>///<paramname="contentAlignment">对齐方式</param>///<returns>返回已经缩放的图片。</returns>publicstaticBitmapThumbnail(Bitmapimage,Sizesize,ThumbModemode,ContentAlignmentcontentAlignment){if(!size.IsEmpty&&!image.Size.IsEmpty&&!size.Equals(image.Size)){//先取一个宽比例。doublescale=(double)image.Width/(double)size.Width;//缩略模式switch(mode){caseThumbMode.Full:if(image.Height>image.Width)scale=(double)image.Height/(double)size.Height;break;caseThumbMode.Max:if(image.Height/scale<size.Height)scale=(double)image.Height/(double)size.Height;break;}SizeFnewSzie=newSizeF((float)(image.Width/scale),(float)(image.Height/scale));Bitmapresult=newBitmap(size.Width,size.Height);using(Graphicsg=Graphics.FromImage(result)){g.FillRectangle(Brushes.White,newRectangle(newPoint(0,0),size));g.InterpolationMode=InterpolationMode.HighQualityBicubic;g.SmoothingMode=SmoothingMode.HighQuality;g.PixelOffsetMode=PixelOffsetMode.HighQuality;g.CompositingMode=CompositingMode.SourceOver;g.CompositingQuality=CompositingQuality.HighQuality;g.TextRenderingHint=TextRenderingHint.AntiAliasGridFit;//对齐方式RectangleFdestRect;switch(contentAlignment){caseContentAlignment.TopCenter:destRect=newRectangleF(newPointF(-(float)((newSzie.Width-size.Width)*0.5),0),newSzie);break;caseContentAlignment.TopRight:destRect=newRectangleF(newPointF(-(float)(newSzie.Width-size.Width),0),newSzie);break;caseContentAlignment.MiddleLeft:destRect=newRectangleF(newPointF(0,-(float)((newSzie.Height-size.Height)*0.5)),newSzie);break;caseContentAlignment.MiddleCenter:destRect=newRectangleF(newPointF(-(float)((newSzie.Width-size.Width)*0.5),-(float)((newSzie.Height-size.Height)*0.5)),newSzie);break;caseContentAlignment.MiddleRight:destRect=newRectangleF(newPointF(-(float)(newSzie.Width-size.Width),-(float)((newSzie.Height-size.Height)*0.5)),newSzie);break;caseContentAlignment.BottomLeft:destRect=newRectangleF(newPointF(0,-(float)(newSzie.Height-size.Height)),newSzie);break;caseContentAlignment.BottomCenter:destRect=newRectangleF(newPointF(-(float)((newSzie.Width-size.Width)*0.5),-(float)(newSzie.Height-size.Height)),newSzie);break;caseContentAlignment.BottomRight:destRect=newRectangleF(newPointF(-(float)(newSzie.Width-size.Width),-(float)(newSzie.Height-size.Height)),newSzie);break;default:destRect=newRectangleF(newPointF(0,0),newSzie);break;}g.DrawImage(image,destRect,newRectangleF(newPointF(0F,0F),image.Size),GraphicsUnit.Pixel);image.Dispose();}returnresult;}elsereturnimage;}///<summary>///保存图片。///</summary>///<paramname="image">要保存的图片</param>///<paramname="quality">品质(1L~100L之间,数值越大品质越好)</param>///<paramname="filename">保存路径</param>publicstaticvoidSaveIamge(Bitmapimage,longquality,stringfilename){using(EncoderParametersencoderParams=newEncoderParameters(1)){using(EncoderParameterparameter=(encoderParams.Param[0]=newEncoderParameter(Encoder.Quality,quality))){ImageCodecInfoencoder=null;//取得扩展名stringext=Path.GetExtension(filename);if(string.IsNullOrEmpty(ext))ext=".jpg";//根据扩展名得到解码、编码器foreach(ImageCodecInfocodecInfoinImageCodecInfo.GetImageEncoders()){if(Regex.IsMatch(codecInfo.FilenameExtension,string.Format(@"(;|^)*{0}(;|$)",ext),RegexOptions.IgnoreCase)){encoder=codecInfo;break;}}Directory.CreateDirectory(Path.GetDirectoryName(filename));image.Save(filename,encoder,encoderParams);}}}///<summary>///保存图片。///</summary>///<paramname="stream">要保存的流</param>///<paramname="quality">品质(1L~100L之间,数值越大品质越好)</param>///<paramname="filename">保存路径</param>publicstaticvoidSaveIamge(Streamstream,longquality,stringfilename){using(BitmapbmpTemp=newBitmap(stream)){SaveIamge(bmpTemp,quality,filename);}}}具体请看这里:
解决方案
解决方案二:
分享..........
解决方案三:
LZ是个好人
解决方案四:
谢谢楼上支持!!
解决方案五:
啊啊啊啊啊啊?好不容易发个帖被列为精了,居然给删了,太过分了!!
解决方案六:
收藏了
解决方案七:
占个位置看看,支持下
解决方案八:
好东西。不过不太清楚怎么用呢。郁闷,要仔细看看。
解决方案九:
谢谢各位支持
解决方案十:
Thankyou~!
解决方案十一:
谢谢
解决方案十二:
收藏了
解决方案十三:
谢谢楼上支持!!
解决方案十四:
UP
解决方案十五:
up.....
解决方案:
up
解决方案:
好东西啊!!!
解决方案:
支持。。。
解决方案:
up
解决方案:
感谢LZ
解决方案:
谢谢
解决方案:
不用谢~!!!UP!!