/// <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