天天看點

.net 裁剪圖檔(不壓縮)

命名空間:

using System.Drawing;
using System.Drawing.Imaging;      
/// <summary>  
        /// 生成圖檔縮略檔案  
        /// </summary>  
        /// <param name="originalImage">圖檔源檔案</param>  
        /// <param name="width">縮略圖寬度</param>  
        /// <param name="height">縮略圖高度</param>  
        /// <param name="mode">生成縮略圖的方式</param>  
        /// <returns>縮率處理後圖檔檔案</returns> 
        public static System.Drawing.Image MakeThumbnail(System.Drawing.Image originalImage, int width, int height, ThumbnailModel mode)
        {
            int towidth = width;
            int toheight = height;

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

            switch (mode)
            {
                case ThumbnailModel.HighWidth: //指定高寬縮放(可能變形)   
                    break;
                case ThumbnailModel.Width: //指定寬,高按比例   
                    toheight = originalImage.Height * width / originalImage.Width;
                    break;
                case ThumbnailModel.Hight: //指定高,寬按比例  
                    towidth = originalImage.Width * height / originalImage.Height;
                    break;
                case ThumbnailModel.Default: //指定高,寬按比例  
                    if (ow <= towidth && oh <= toheight)
                    {
                        x = -(towidth - ow) / 2;
                        y = -(toheight - oh) / 2;
                        ow = towidth;
                        oh = toheight;
                    }
                    else
                    {
                        if (ow > oh)//寬大于高
                        {
                            x = 0;
                            y = -(ow - oh) / 2;
                            oh = ow;
                        }
                        else//高大于寬
                        {
                            y = 0;
                            x = -(oh - ow) / 2;
                            ow = oh;
                        }
                    }
                    break;
                case ThumbnailModel.Auto:
                    if (originalImage.Width / originalImage.Height >= width / height)
                    {
                        if (originalImage.Width > width)
                        {
                            towidth = width;
                            toheight = (originalImage.Height * width) / originalImage.Width;
                        }
                        else
                        {
                            towidth = originalImage.Width;
                            toheight = originalImage.Height;
                        }
                    }
                    else
                    {
                        if (originalImage.Height > height)
                        {
                            toheight = height;
                            towidth = (originalImage.Width * height) / originalImage.Height;
                        }
                        else
                        {
                            towidth = originalImage.Width;
                            toheight = originalImage.Height;
                        }
                    }
                    break;
                case ThumbnailModel.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.White);

            //在指定位置并且按指定大小繪制原圖檔的指定部分  
            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                        new System.Drawing.Rectangle(x, y, ow, oh),
                        System.Drawing.GraphicsUnit.Pixel);

            return bitmap;
        }

        /// <summary>
        /// 縮率圖處理模式
        /// </summary>
        public enum ThumbnailModel
        {
            /// <summary>
            /// 指定高寬縮放(可能變形)   
            /// </summary>
            HighWidth,

            /// <summary>
            /// 指定寬,高按比例   
            /// </summary>
            Width,

            /// <summary>
            /// 預設  全圖不變形   
            /// </summary>
            Default,

            /// <summary>
            /// 指定高,寬按比例
            /// </summary>
            Hight,

            /// <summary>
            /// 指定高寬裁減(不變形)??指定裁剪區域
            /// </summary>
            Cut,

            /// <summary>
            /// 自動 原始圖檔按比例縮放
            /// </summary>
            Auto
        }      

繼續閱讀