High-quality Image Compression

Source: Internet
Author: User
  • # Region using directives
  • Using system;
  • Using system. Web;
  • Using system. drawing;
  • Using system. Drawing. imaging;
  • Using system. Drawing. drawing2d;
  •  
  • # Endregion using directives
  • Public class imageresizer
  • {
  • # Region Fields
  • Static httpserverutility Server = httpcontext. Current. server;
  •  
  • # Endregion Fields
  •  
  • # Region get Encoder
  • Public static imagecodecinfo getencoder (string mimetype)
  • {
  • Foreach (imagecodecinfo Codec in imagecodecinfo. getimageencoders ())
  • {
  • If (codec. mimetype = mimetype)
  • {
  • Return codec;
  • }
  • }
  • Return NULL;
  • }
  •  
  • # Endregion get Encoder
  •  
  • # Region resize Image
  • Public static void resizeimage (string originalimagelocation,
  • Int width, int height, string newimagelocation)
  • {
  • Image originalimage = image. fromfile (
  • Server. mappath (originalimagelocation. Replace ("//","/")));
  • Resizeimage (originalimage, width, height, newimagelocation );
  • }
  • Public static void resizeimage (image originalimage,
  • Int width, int height, string newimagelocation)
  • {
  • //
  • // Scale the image depending on whether it is portrait or landscape.
  • Decimal newwidth;
  • Decimal newheight;
  • If (originalimage. width/width)> (originalimage. width/height ))
  • {
  • Newwidth = width;
  • Newheight = (int32) math. Ceiling (convert. todecimal ((
  • Originalimage. Height * newwidth)/originalimage. width ));
  • If (newheight> height)
  • {
  • Newwidth = newwidth * (height/newheight );
  • Newheight = height;
  • }
  • }
  • Else
  • {
  • Newheight = height;
  • Newwidth = math. Ceiling (convert. todecimal ((
  • Originalimage. Width * newheight)/originalimage. Height ));
  • If (newwidth> width)
  • {
  • Newheight = newheight * (width/newwidth );
  • Newwidth = width;
  • }
  • }
  • //
  • // Create the new image as a bitmap
  • Bitmap newbitmap = new Bitmap (convert. toint32 (newwidth ),
  • Convert. toint32 (newheight), pixelformat. format16bpprgb565 );
  • Graphics G = graphics. fromimage (newbitmap );
  • G. interpolationmode = interpolationmode. highqualitybicubic;
  • G. smoothingmode = smoothingmode. antialias;
  • G. smoothingmode = smoothingmode. highquality;
  • G. drawimage (originalimage, 0, 0, convert. toint32 (newwidth ),
  • Convert. toint32 (newheight ));
  • //
  • // Save the image using the appropriate encoder with a quality of 98%
  • Imagecodecinfo codecencoder = imageresizer. getencoder ("image/JPEG ");
  • Int Quality = 98;
  • Encoderparameters encodeparams = new encoderparameters (1 );
  • Encoderparameter qualityparam = new encoderparameter (encoder. quality, quality );
  • Encodeparams. Param [0] = qualityparam;
  • Newbitmap. setresolution (72, 72 );
  • Newbitmap. Save (server. mappath (newimagelocation), codecencoder, encodeparams );
  • //
  • // Clean up
  • G. Dispose ();
  • Newbitmap. Dispose ();
  • Originalimage. Dispose ();
  • }
  •  
  • # Endregion resize Image
  • }
  • 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.