C # cut produces high-quality scaled pictures

Source: Internet
Author: User

/// <summary>        ///High-quality zoom picture/// </summary>        /// <param name= "Originfilepath" >path of the source graph</param>        /// <param name= "Targetfilepath" >path to store thumbnails</param>        /// <param name= "Destwidth" >picture width after zooming</param>        /// <param name= "Destheight" >image height after zooming</param>        /// <returns>Indicates whether this operation was successful</returns>         Public Static BOOLTosuolue (System.IO.Stream OStream,stringTargetfilepath,intTowidth,inttoheight) {            //System.Drawing.Image originimage = System.Drawing.Image.FromFile (Originfilepath);System.Drawing.Image Originimage =System.Drawing.Image.FromStream (OStream); System.Drawing.Imaging.ImageFormat Thisformat=Originimage.rawformat; //Scale by scaling            intx =0, y =0; intow =Originimage.width; intOh =Originimage.height; if((Double) OW/(Double) Oh > (Double) Towidth/(Double) toheight) {Oh=Originimage.height; ow= Originimage.height * Towidth/Toheight; Y=0; X= (Originimage.width-ow)/2; }            Else{ow=Originimage.width; Oh= Originimage.width * Toheight/Towidth; X=0; Y= (Originimage.height-oh)/2; } Bitmap BT=NewBitmap (Towidth, toheight);//Create a bitmap instance based on the specified size            using(Graphics g =Graphics.fromimage (BT))                {g.clear (color.white); //set the drawing quality of the canvasG.compositingquality =System.Drawing.Drawing2D.CompositingQuality.HighQuality; G.smoothingmode=System.Drawing.Drawing2D.SmoothingMode.HighQuality; G.interpolationmode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; G.drawimage (Originimage,NewRectangle (0,0, Towidth, toheight),NewSystem.Drawing.Rectangle (x, Y, Ow, OH), System.Drawing.GraphicsUnit.Pixel);            G.dispose (); } System.Drawing.Imaging.EncoderParameters encoderparams=NewSystem.Drawing.Imaging.EncoderParameters ();//get the built -in encoder            Long[] quality =New Long[1]; quality[0] = -; System.Drawing.Imaging.EncoderParameter Encoderparam=NewSystem.Drawing.Imaging.EncoderParameter (System.Drawing.Imaging.Encoder.Quality, quality); encoderparams.param[0] =Encoderparam; //Try//{                //get the ImageCodecInfo object that contains information about the built-in image codecsystem.drawing.imaging.imagecodecinfo[] Arrayici =System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders (); System.Drawing.Imaging.ImageCodecInfo Jpegici=NULL;  for(inti =0; i < arrayici.length; i++)                {                    if(Arrayici[i]. Formatdescription.equals ("JPEG") ) {Jpegici= Arrayici[i];//set to JPEG encoding                         Break; }                }                if(Jpegici! =NULL)//Save thumbnail image{bt.                Save (Targetfilepath, Jpegici, encoderparams); }                Else{bt.                Save (Targetfilepath, Thisformat);                } originimage.dispose (); return true; //}            //catch (System.Runtime.InteropServices.ExternalException E1)//GDI + General error occurred//{            //return false; //}            //catch (Exception E2)//{            //return false; //}}

C # cut produces high-quality scaled pictures

Related Article

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.