. NET equal width, equal height, equal proportion, fixed width high generated thumbnail class

Source: Internet
Author: User
Tags bmp image

generate a thumbnail image based on the original image#regionGenerate a thumbnail image based on the original image/// <summary>          ///generate thumbnails based on source images/// </summary>          /// <param name= "Imgpath_old" >Source Graph (large image) physical path</param>          /// <param name= "Imgpath_new" >Thumbnail physical path (the resulting thumbnail will be saved to that physical location)</param>          /// <param name= "width" >thumbnail width</param>          /// <param name= "height" >thumbnail height</param>          /// <param name= "mode" >thumbnail zoom mode (value "HW": Specify aspect scale, may deform; value "W": proportionally scaled by the specified width, and value "H": scaled by the specified height, width proportionally; "Cut": cut by the specified height and width, not deformed); Value "DB": equal to scale, Scale proportionally to a larger value as a standard</param>          /// <param name= "type" >extension of the file that will generate the thumbnail (only: JPG, GIF, PNG, BMP)</param>           Public Static voidMakethumbnail (stringImgpath_old,stringImgpath_new,intWidthintHeightstringModestringImageType,intXxintyy) {System.Drawing.Image img=System.Drawing.Image.FromFile (Imgpath_old); intTowidth = width;intToheight =height; intx =0;inty =0;intow =img.            Width; intOh = img. Height;Switch(mode) { Case "HW"://Specify aspect compression                    if((Double) img. Width/(Double) img. Height > (Double) Width/(Double) height)//determine what shape is a graphic{towidth=width; Toheight= img. Height * Width/img.                    Width; }                    Else if((Double) img. Width/(Double) img. Height = = (Double) Width/(Double) (height) {towidth=width; Toheight=height; }                    Else{toheight=height; Towidth= img. Width * Height/img.                    Height; }                     Break;  Case "W"://specify wide, high proportionallyToheight = img. Height * Width/img.                    Width;  Break;  Case "H"://specify high, wide proportionallyTowidth = img. Width * Height/img.                    Height;  Break;  Case "Cut"://designation Aspect cut (not deformed)                    if((Double) img. Width/(Double) img. Height > (Double) Towidth/(Double) toheight) {Oh=img.                        Height; ow= img. Height * Towidth/Toheight; Y= yy; x = (img. Width-ow)/2; }                    Else{ow=img.                        Width; Oh= img. Width * Height/Towidth; X= XX; y = (img. HEIGHT-OH)/2; }  Break;  Case "DB"://scale by a larger value (no distortion)                    if((Double) img. Width/(Double) Towidth < (Double) img. Height/(Double) {toheight) {toheight=height; Towidth= img. Width * Height/img.                    Height; }                    Else{towidth=width; Toheight= img. Height * Width/img.                    Width; }  Break; default:                     Break; }            //Create a new BMP imageSystem.Drawing.Image bitmap =NewSystem.Drawing.Bitmap (Towidth, toheight); //Create a new artboardSystem.Drawing.Graphics g =System.Drawing.Graphics.FromImage (bitmap); //setting high-quality interpolation methodsG.interpolationmode =System.Drawing.Drawing2D.InterpolationMode.High; //set high quality, low speed rendering smoothnessG.smoothingmode =System.Drawing.Drawing2D.SmoothingMode.HighQuality; //Empty the canvas and fill it with a transparent background colorg.clear (System.Drawing.Color.Transparent); //draws the specified portion of the original picture at the specified position and at the specified sizeG.drawimage (IMG,NewSystem.Drawing.Rectangle (0,0, Towidth, toheight),NewSystem.Drawing.Rectangle (x, Y, Ow, OH), System.Drawing.GraphicsUnit.Pixel); Try            {                //save thumbnails in jpg format                Switch(Imagetype.tolower ()) { Case "gif": IMG. Save (Imgpath_new, imageformat.jpeg);//generate thumbnail images                         Break;  Case "jpg": Bitmap.                        Save (Imgpath_new, System.Drawing.Imaging.ImageFormat.Jpeg);  Break;  Case "BMP": Bitmap.                        Save (Imgpath_new, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.BMP);  Break;  Case "PNG": Bitmap.                        Save (Imgpath_new, System.Drawing.Imaging.ImageFormat.Png);  Break; default: Bitmap.                        Save (Imgpath_new, System.Drawing.Imaging.ImageFormat.Jpeg);  Break; }                ////Save thumbnail image                //bitmap. Save (imgpath_new);            }            Catch(System.Exception e) {Throwe; }            finally{img.                Dispose (); Bitmap. Dispose ();            G.dispose (); }        }        #endregion

. NET equal width, equal height, equal proportion, fixed width high generated thumbnail class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.