Generate thumbnails of high-quality small spaces C # code

Source: Internet
Author: User
Using system;using system.collections.generic;using system.linq;using system.text;using System.Drawing;using System.IO;        Namespace Consoleapplication3_thumbnailimg{class Program {const int HEIGHT = 190;        const int WIDTH = 190;            static void Main (string[] args) {string srcimgpath = @ "G:\photoes\20060725\Picture 057.jpg";             String targetfolder = @ "E:\Diary\Test\thumbnail";            Setthumbnail_1 (Srcimgpath, TargetFolder);            Setthumbnail_2 (Srcimgpath, TargetFolder);             Setthumbnail_3 (Srcimgpath, TargetFolder);        Console.read (); } static void Setthumbnail_1 (String srcimgpath, String targetfolder) {using (Bitmap Source = NE                    W Bitmap (Srcimgpath)) {//return the source image if it ' s smaller than the designated thumbnail                int WI, HI;                WI = WIDTH;                 hi = HEIGHT; Maintain the aspect ratio desPite the thumbnail size parameters if (source. Width > Source.                    Height) {wi = WIDTH; hi = (int) (source. Height * (decimal) Width/source.                Width));                    } else {hi = HEIGHT; WI = (int) (source. Width * ((decimal) Height/source.                Height)); } using (Image thumb = source. Getthumbnailimage (WI, Hi, null, IntPtr.Zero)) {string TargetPath = Path.Combine (targetf                    Older, "th_1.jpg"); Thumb.                Save (TargetPath); }}}} static void Setthumbnail_2 (String srcimgpath, String targetfolder) {USI Ng (Bitmap Source = new Bitmap (Srcimgpath)) {//return the source image if it ' s smaller than th                e designated thumbnail int wi, hi;                WI = WIDTH;   hi = HEIGHT;              Maintain the aspect ratio despite the thumbnail size parameters if (source. Width > Source.                    Height) {wi = WIDTH; hi = (int) (source. Height * (decimal) Width/source.                Width));                    } else {hi = HEIGHT; WI = (int) (source. Width * ((decimal) Height/source.                Height)); }//Original code that creates lousy thumbnails//System.Drawing.Image ret = source.                   Getthumbnailimage (Wi,hi,null,intptr.zero); using (System.Drawing.Bitmap thumb = new Bitmap (WI, HI)) {using (Graphics g = Graphics. FromImage (thumb)) {G.interpolationmode = System.Drawing.Drawing2D.Interpolation                        Mode.highqualitybicubic;                        G.fillrectangle (brushes.white, 0, 0, WI, HI); G.drawimage (Source, 0, 0, WI, HI);                    } String TargetPath = Path.Combine (TargetFolder, "th_2.jpg"); Thumb.                Save (TargetPath); }}} static void Setthumbnail_3 (String srcimgpath, String targetfolder) {// Configure JPEG Compression Engine System.Drawing.Imaging.EncoderParameters encoderparams = new System.drawin            G.imaging.encoderparameters ();            long[] quality = new LONG[1];            Quality[0] = 75; System.Drawing.Imaging.EncoderParameter Encoderparam = new System.Drawing.Imaging.EncoderParameter (            System.Drawing.Imaging.Encoder.Quality, quality);             Encoderparams.param[0] = Encoderparam;            system.drawing.imaging.imagecodecinfo[] Arrayici = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders ();            System.Drawing.Imaging.ImageCodecInfo Jpegici = null;           for (int x = 0; x < arrayici.length; × x + +) {     if (Arrayici[x].                    Formatdescription.equals ("JPEG")) {Jpegici = arrayici[x];                Break                }} using (Bitmap Source = new Bitmap (Srcimgpath)) {int WI, HI;                WI = WIDTH;                 hi = HEIGHT; Maintain the aspect ratio despite the thumbnail size parameters if (source. Width > Source.                    Height) {wi = WIDTH; hi = (int) (source. Height * (decimal) Width/source.                Width));                    } else {hi = HEIGHT; WI = (int) (source. Width * ((decimal) Height/source.                Height)); }//Original code that creates lousy thumbnails//System.Drawing.Image ret = source.                   Getthumbnailimage (Wi,hi,null,intptr.zero); using (System.Drawing.Bitmap thumb = new Bitmap(WI, HI)) {using (Graphics g = graphics.fromimage (thumb)) {G.interpol                        Ationmode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                        G.fillrectangle (brushes.white, 0, 0, WI, HI);                    G.drawimage (source, 0, 0, WI, HI);                    } String TargetPath = Path.Combine (TargetFolder, "th_3.jpg"); Thumb.                Save (TargetPath, Jpegici, encoderparams); }             }         }    }}
  • 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.