asp.Net高品質サムネイル汎用関数(c#コード)を生成し、多種の生成方式をサポートする

3600 ワード

      ,                  .   asp     com    ,   .net                 .         (     ),           .net sdk    .QQROOM               4     .



        /**//// <summary>

        ///      

        /// </summary>

        /// <param name="originalImagePath">    (    )</param>

        /// <param name="thumbnailPath">     (    )</param>

        /// <param name="width">     </param>

        /// <param name="height">     </param>

        /// <param name="mode">        </param>    

        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)

        {

            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 (mode)

            {        

                case "HW"://      (    )                

                    break;

                case "W"://   ,                        

                    toheight = originalImage.Height * width/originalImage.Width;

                    break;

                case "H"://   ,    

                    towidth = originalImage.Width * height/originalImage.Height;                    

                    break;        

                case "Cut"://      (   )                

                    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;

            }    

            

            //    bmp  

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



            //      

            Graphics g = System.Drawing.Graphics.FromImage(bitmap);



            //        

            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;



            //     ,         

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;



            //             

            g.Clear(Color.Transparent);        



            //                      

            g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), 

                new Rectangle(x, y, ow,oh),

                GraphicsUnit.Pixel);



            try

            {            

                // jpg       

                bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);

            }

            catch(System.Exception e)

            {

                throw e;

            }

            finally

            {

                originalImage.Dispose();

                bitmap.Dispose();                        

                g.Dispose();

            }

        }