C # Change picture size (compression), Image Resize

Source: Internet
Author: User

/// <summary>    ///Resize image with a directory as source/// </summary>    /// <param name= "Originalfilelocation" >Image Location</param>    /// <param name= "Heigth" >New Height</param>    /// <param name= "width" >New Width</param>    /// <param name= "Keepaspectratio" >Keep the aspect ratio</param>    /// <param name= "Getcenter" >return the center bit of the image</param>    /// <returns>image with new dimentions</returns>     PublicImage Resizeimagefromfile (String originalfilelocation,intHeigth,intwidth, Boolean keepaspectratio, Boolean getcenter) {        intNewheigth =Heigth; System.Drawing.Image Fullsizeimage=System.Drawing.Image.FromFile (originalfilelocation); //Prevent using images internal thumbnailFullsizeimage.rotateflip (System.Drawing.RotateFlipType.Rotate180FlipNone);        Fullsizeimage.rotateflip (System.Drawing.RotateFlipType.Rotate180FlipNone); if(Keepaspectratio | |getcenter) {            intBmpy =0; DoubleResize = (Double) Fullsizeimage.width/(Double) width;//get the resize vector            if(getcenter) {bmpy= (int) ((Fullsizeimage.height-(Heigth * resize))/2);//gives the Y value of the part that would be cut off and to show only the part in the centerRectangle section =NewRectangle (NewPoint (0, Bmpy),NewSize (Fullsizeimage.width, (int) (Heigth * resize)));//Create the section to cut of the original image//System.Console.WriteLine ("the section that would be a cut off:" + section.) Size.tostring () + "The Y value is minimized by:" + bmpy);Bitmap orimg =NewBitmap ((Bitmap) fullsizeimage);//For the correct effect convert image to bitmap.Fullsizeimage.dispose ();//clear the original image                using(Bitmap tempimg =NewBitmap (section. Width, section. Height) {Graphics cutimg= Graphics.fromimage (tempimg);//set the file to save the new image to.Cutimg.drawimage (Orimg,0,0, section, GraphicsUnit.Pixel);//cut the image and save it to tempimgFullsizeimage = tempimg;//Save the Tempimg as Fullsizeimage for resizing laterOrimg.dispose ();                    Cutimg.dispose (); returnFullsizeimage.getthumbnailimage (width, heigth,NULL, IntPtr.Zero); }            }            ElseNewheigth = (int) (fullsizeimage.height/resize);//set the new Heigth of the current image}//return the image resized to the given heigth and width        returnFullsizeimage.getthumbnailimage (width, newheigth,NULL, IntPtr.Zero); }     /// <summary>    ///Resize image with a directory as source/// </summary>    /// <param name= "Originalfilelocation" >Image Location</param>    /// <param name= "Heigth" >New Height</param>    /// <param name= "width" >New Width</param>    /// <returns>image with new dimentions</returns>     PublicImage Resizeimagefromfile (String originalfilelocation,intHeigth,intwidth) {        returnResizeimagefromfile (Originalfilelocation, heigth, Width,false,false); }    /// <summary>    ///Resize image with a directory as source/// </summary>    /// <param name= "Originalfilelocation" >Image Location</param>    /// <param name= "Heigth" >New Height</param>    /// <param name= "width" >New Width</param>    /// <param name= "Keepaspectratio" >Keep the aspect ratio</param>    /// <returns>image with new dimentions</returns>     PublicImage Resizeimagefromfile (String originalfilelocation,intHeigth,intwidth, Boolean keepaspectratio) {        returnResizeimagefromfile (Originalfilelocation, heigth, width, keepaspectratio,false); }

The method is very good, can maintain a high-wide ratio, you can also be proportional to the original picture in the middle

Reference: Http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp

C # Change picture size (compression), Image Resize

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.