C#5 to generate thumbnails

Source: Internet
Author: User
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Drawing;

Using System.IO;
            Namespace MyTest {class Imgthumbnail {public enum mode {W, H, HW, cut, MAXHW} public Imgthumbnail () {}///<sum mary>///generate thumbnails///</summary>///<param name= "Originalimagepath" > Source map Path (physical path) </p aram>///<param name= "Thumbnailpath" > Thumbnail path (physical path) </param>///<param name= "width" > Thumbnail Width </param>///<param name= "height" > Thumbnail height </param>///<param name= "mode" > How to Generate Thumbnails &
        lt;/param> public static Image Makethumbnail (string originalimagepath, int width, int height, mode m) {if (!
            File.exists (Originalimagepath)) {return null; } Image Originalimage = Image.FromFile (Originalimagepath);
            int towidth = width;

            int toheight = height;
            int x = 0;
            int y = 0;
            int ow = Originalimage.width;

            int oh = originalimage.height; Switch (m) {case mode. Maxhw:if ((Ow/width) > (ow/height)) {towidth = WI
                        Dth
                        Toheight = (Int32) math.ceiling (Convert.todecimal ((OH * towidth)/ow)); if (Toheight > height) {towidth = Convert.ToInt32 (Towidth * (conve Rt.
                            ToDecimal (height)/toheight));
                        Toheight = height;
                        } else {toheight = height;
  Towidth = (Convert.ToInt32 (math.ceiling convert.todecimal (OW * toheight))/OH);                      if (Towidth > width) {toheight = Convert.toi
                            Nt32 (Toheight * convert.todecimal (width)/towidth);
                        Towidth = width;
                }} break; Case mode.
                HW://Specifies the width scaling (possibly deformed) break; Case mode.
                    w://specified wide, high proportionally toheight = Originalimage.height * width/originalimage.width;
                Break Case mode.
                    h://specified high, wide proportional towidth = Originalimage.width * height/originalimage.height;
                Break Case mode. CUT://Specifies width cut (not deformed) if (double) originalimage.width/(double) originalimage.height >
                        (double) towidth/(double) toheight) {oh = Originalimage.height; ow = Originalimage.Height * towidth/toheight;
                        y = 0;
                    x = (Originalimage.width-ow)/2;
                        else {ow = Originalimage.width;
                        Oh = originalimage.width * height/towidth;
                        x = 0;
                    y = (Originalimage.height-oh)/2;
                } break;
            Default:break; } towidth = towidth > ow?
            Ow:towidth; Toheight = toheight > Oh?
            Oh:toheight;
            if (Toheight = = 0 | | towidth = 0) {return null;

            //new BMP picture Image bitmap = new System.Drawing.Bitmap (towidth, toheight);

            Create a new artboard Graphics g = System.Drawing.Graphics.FromImage (bitmap); Set high quality interpolation method G.interpolationmode = System.Drawing.Drawing2D.

            Interpolationmode.high;

            Set high quality, low speed rendering smooth degree g.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            Empty the canvas and fill the g.clear with a transparent background color (color.transparent); Draws the specified portion of the original picture at the specified location and at the specified size g.drawimage (originalimage, New Rectangle (0, 0, Towidth, toheight), NE

            W Rectangle (x, Y, Ow, OH), graphicsunit.pixel);
                try {return bitmap; Save thumbnail//bitmap in jpg format.
            Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg);
                catch (System.Exception e) {return null;
            Throw e;
                finally {originalimage.dispose (); Bitmap.
                Dispose ();
            G.dispose ();
 }
        }
    }
}

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.