画像サムネイルの生成

3917 ワード

        #region     ,          
        /// <summary>
        ///     
        /// </summary>
        /// <param name="originalImagePath">      , :c:\\images\\1.gif</param>
        /// <param name="thumbnailPath">         , :c:\\images\\2.gif</param>
        /// <param name="width"> </param>
        /// <param name="height"> </param>
        /// <param name="mode">EQU:           ;HW://      (    );W:   ,    ;H:   ,    ;Cut:      (   )</param>
        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

            int towidth = width;
            int toheight = height;

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            if (mode == "EQU")//      ,     
            {
                //if(height/oh>width/ow),      ,      ;       ,      
                if (height * ow > width * oh)
                {
                    mode = "W";
                }
                else
                {
                    mode = "H";
                }
            }
            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     
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

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

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

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

            //                
            g.Clear(System.Drawing.Color.Transparent);

            //                         
            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                new System.Drawing.Rectangle(x, y, ow, oh),
                System.Drawing.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();
            }
        }
        #endregion