/// <summary> ///generate thumbnail images/// </summary> /// <param name= "Orginalimagepat" >Original image Address</param> /// <param name= "Thumnailpath" >thumbnail Address</param> /// <param name= "width" >thumbnail width</param> /// <param name= "height" >thumbnail height</param> /// <param name= "model" >mode of generating thumbnails</param> Public voidMakethumnail (stringOriginalimagepath,stringThumnailpath,intWidthintHeightstringmodel) {System.Drawing.Image Originalimage=System.Drawing.Image.FromFile (Originalimagepath); intThumwidth = width;//the width of the thumbnail image intThumheight = height;//the height of the thumbnail image intx =0; inty =0; intOriginalWidth = Originalimage.width;//the width of the original picture intOriginalHeight = Originalimage.height;//the height of the original picture Switch(model) { Case "HW"://Specify aspect scaling, possibly deformed Break; Case "W"://specify width, height scaled by scaleThumheight = originalimage.height * Width/Originalimage.width; Break; Case "H"://specify height, width scaled by equal scaleThumwidth = originalimage.width * Height/Originalimage.height; Break; Case "Cut": if((Double) Originalimage.width/(Double) Originalimage.height > (Double) Thumwidth/(Double) {thumheight) {originalheight=Originalimage.height; OriginalWidth= Originalimage.height * Thumwidth/Thumheight; Y=0; X= (originalimage.width-originalwidth)/2; } Else{originalwidth=Originalimage.width; OriginalHeight= OriginalWidth * Height/Thumwidth; X=0; Y= (originalimage.height-originalheight)/2; } Break; default: Break; } //Create a new BMP imageSystem.Drawing.Image bitmap =NewSystem.Drawing.Bitmap (Thumwidth, thumheight); //Create a new artboardSystem.Drawing.Graphics graphic =System.Drawing.Graphics.FromImage (bitmap); //set up a high-quality check methodGraphic. Interpolationmode =System.Drawing.Drawing2D.InterpolationMode.High; //set high quality, low speed rendering smoothnessGraphic. SmoothingMode =System.Drawing.Drawing2D.SmoothingMode.HighQuality; //Empty the canvas and fill it with a transparent background colorgraphic. Clear (System.Drawing.Color.Transparent); //draws the specified portion of the original picture at the specified position and at the specified sizeGraphic. DrawImage (Originalimage,NewSystem.Drawing.Rectangle (0,0, Thumwidth, thumheight),NewSystem.Drawing.Rectangle (x, Y, OriginalWidth, originalheight), System.Drawing.GraphicsUnit.Pixel); Try{bitmap. Save (Thumnailpath, System.Drawing.Imaging.ImageFormat.Jpeg); } Catch(Exception ex) {Throwex; } finally{originalimage.dispose (); Bitmap. Dispose (); Graphic. Dispose (); } }
ASP. NET generated thumbnail image