customizing crop pictures

Source: Internet
Author: User

Determine the size of the picture, scale the picture according to the clipping target, and then crop the picture (in the case of the image without distortion)

        /// <summary>        ///Specify the length-width cropping///crop a picture to the maximum extent of the stencil scale and zoom to the stencil size/// </summary>        /// <param name= "FromFile" >Original Stream Object</param>        /// <param name= "Filesaveurl" >Save Path</param>        /// <param name= "MaxWidth" >Max width (unit: px)</param>        /// <param name= "MaxHeight" >Max height (unit: PX)</param>        /// <param name= "Quality" >quality (range 0-100)</param>         Public Static voidCutforcustom (Image initimage,stringFilesaveurl,intMaxWidth,intMaxHeight,intquality) {            //get the original picture from the file and use the embedded color management information in the stream//System.Drawing.Image initimage = System.Drawing.Image.FromStream (FromFile, true); //the original width and height are smaller than the template, not for processing, directly save            if(Initimage.width <= maxWidth && initimage.height <=maxheight)            {Initimage.save (Filesaveurl, System.Drawing.Imaging.ImageFormat.Jpeg); }            Else            {                //the width and height ratio of the template                DoubleTemplaterate = (Double) MaxWidth/MaxHeight; //the width and height ratio of the original picture                DoubleInitrate = (Double) Initimage.width/Initimage.height; //The original image is equal to the template, directly scaled                if(Templaterate = =initrate) {                    //generate final picture by template sizeSystem.Drawing.Image Templateimage =NewSystem.Drawing.Bitmap (MaxWidth, maxheight); System.Drawing.Graphics Templateg=System.Drawing.Graphics.FromImage (templateimage); Templateg.interpolationmode=System.Drawing.Drawing2D.InterpolationMode.High; Templateg.smoothingmode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;                    Templateg.clear (Color.White); Templateg.drawimage (Initimage,NewSystem.Drawing.Rectangle (0,0, MaxWidth, MaxHeight),NewSystem.Drawing.Rectangle (0,0, Initimage.width, initimage.height), System.Drawing.GraphicsUnit.Pixel);                Templateimage.save (Filesaveurl, System.Drawing.Imaging.ImageFormat.Jpeg); }                //The original image is different from the template, scaled after cropping                Else                {                    //Crop ObjectsSystem.Drawing.Image Pickedimage =NULL; System.Drawing.Graphics PICKEDG=NULL; //positioningRectangle FROMR =NewRectangle (0,0,0,0);//Original crop positioningRectangle ToR =NewRectangle (0,0,0,0);//Target Positioning//wide to the standard for cropping                    if(Templaterate >initrate) {                        //Cropping Object InstantiationPickedimage =NewSystem.Drawing.Bitmap (Initimage.width, (int) System.Math.Floor (Initimage.width/templaterate)); PICKEDG=System.Drawing.Graphics.FromImage (pickedimage); //Clipping source PositioningFromr.x =0; Fromr.y= (int) System.Math.Floor ((initimage.height-initimage.width/templaterate)/2); Fromr.width=Initimage.width; Fromr.height= (int) System.Math.Floor (Initimage.width/templaterate); //clipping target positioningTor.x =0; Tor.y=0; Tor.width=Initimage.width; Tor.height= (int) System.Math.Floor (Initimage.width/templaterate); }                    //high for standard cropping                    Else{pickedimage=NewSystem.Drawing.Bitmap ((int) System.Math.Floor (Initimage.height *templaterate), initimage.height); PICKEDG=System.Drawing.Graphics.FromImage (pickedimage); Fromr.x= (int) System.Math.Floor ((Initimage.width-initimage.height * templaterate)/2); Fromr.y=0; Fromr.width= (int) System.Math.Floor (Initimage.height *templaterate); Fromr.height=Initimage.height; Tor.x=0; Tor.y=0; Tor.width= (int) System.Math.Floor (Initimage.height *templaterate); Tor.height=Initimage.height; }                    //Set QualityPickedg.interpolationmode =System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; Pickedg.smoothingmode=System.Drawing.Drawing2D.SmoothingMode.HighQuality; //croppingpickedg.drawimage (Initimage, ToR, FROMR, System.Drawing.GraphicsUnit.Pixel); //generate final picture by template sizeSystem.Drawing.Image Templateimage =NewSystem.Drawing.Bitmap (MaxWidth, maxheight); System.Drawing.Graphics Templateg=System.Drawing.Graphics.FromImage (templateimage); Templateg.interpolationmode=System.Drawing.Drawing2D.InterpolationMode.High; Templateg.smoothingmode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;                    Templateg.clear (Color.White); Templateg.drawimage (Pickedimage,NewSystem.Drawing.Rectangle (0,0, MaxWidth, MaxHeight),NewSystem.Drawing.Rectangle (0,0, Pickedimage.width, pickedimage.height), System.Drawing.GraphicsUnit.Pixel); //Key Quality Control//gets an array of system encoding types, including the Jpeg,bmp,png,gif,tiffimagecodecinfo[] Icis =imagecodecinfo.getimageencoders (); ImageCodecInfo ici=NULL; foreach(ImageCodecInfo IinchICIS) {                        if(I.mimetype = ="Image/jpeg"|| I.mimetype = ="image/bmp"|| I.mimetype = ="Image/png"|| I.mimetype = ="Image/gif") {ici=i; }} encoderparameters EP=NewEncoderParameters (1); Ep. param[0] =NewEncoderparameter (System.Drawing.Imaging.Encoder.Quality, (Long) quality); //Save thumbnail imageTemplateimage.save (Filesaveurl, ICI, EP); //Templateimage.save (Filesaveurl, System.Drawing.Imaging.ImageFormat.Jpeg); //Freeing ResourcesTemplateg.dispose ();                    Templateimage.dispose ();                    Pickedg.dispose ();                Pickedimage.dispose (); }            }            //Freeing ResourcesInitimage.dispose (); }

SOURCE download

customizing crop pictures

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.