1. 程式人生 > >C#常用的圖片處理方法-圖片剪切、圖片壓縮、多圖合並代碼

C#常用的圖片處理方法-圖片剪切、圖片壓縮、多圖合並代碼

brush || 釋放 gef highlight exce summary 根據 sin

        /// <summary>
        /// 圖片轉成圓角方法二
        /// </summary>
        private Bitmap WayTwo(Bitmap bitmap)
        {
            //using (Image i = new Bitmap(file))
            using (Image i = bitmap)
            {
                Bitmap b = new Bitmap(i.Width, i.Height);
                using (Graphics g = Graphics.FromImage(b))
                {
                    g.DrawImage(i, 0, 0, b.Width, b.Height);
                    int r = Math.Min(b.Width, b.Height) / 2;
                    PointF c = new PointF(b.Width / 2.0F, b.Height / 2.0F);
                    for (int h = 0; h < b.Height; h++)
                        for (int w = 0; w < b.Width; w++)
                            if ((int)Math.Pow(r, 2) < ((int)Math.Pow(w * 1.0 - c.X, 2) + (int)Math.Pow(h * 1.0 - c.Y, 2)))
                            {
                                b.SetPixel(w, h, Color.Transparent);
                            }
                }
                return b;
            }
        }

        /// <summary>  
        /// 合並圖片  
        /// </summary>  
        /// <param name="imgBack"></param>  
        /// <param name="img"></param>  
        /// <returns></returns>  
        public static Bitmap CombinImage(Image imgBack, Image img, int xDeviation = 0, int yDeviation = 0)
        {
            Bitmap bmp = new Bitmap(imgBack.Width, imgBack.Height);
            Graphics g = Graphics.FromImage(bmp);

            g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);
            //白色邊框
            // g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 + xDeviation - 1, imgBack.Height / 2 - img.Height / 2 + yDeviation - 1, img.Width + 2, img.Height + 2);
            //填充圖片

            //設置高質量插值法
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //設置高質量,低速度呈現平滑程度
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;

            g.Clear(Color.Transparent);

            //防止出現漸變
            var imgAtt = new ImageAttributes();
            imgAtt.SetWrapMode(WrapMode.TileFlipXY);

            var x = imgBack.Width / 2 - img.Width / 2 + xDeviation;
            var y = imgBack.Height / 2 - img.Height / 2 + yDeviation;
            var width = img.Width;
            var height = img.Height;
            // g.DrawImage(img, imgBack.Width / 2 - img.Width / 2 + xDeviation, imgBack.Height / 2 - img.Height / 2 + yDeviation,img.Width , img.Height);
            g.DrawImage(img, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel, imgAtt);

            GC.Collect();
            return bmp;
        }

        /// <summary>
        /// 從大圖中截取一部分圖片
        /// </summary>
        /// <param name="fromImage">來源圖片</param>        
        /// <param name="offsetX">從偏移X坐標位置開始截取</param>
        /// <param name="offsetY">從偏移Y坐標位置開始截取</param>
        /// <param name="width">保存圖片的寬度</param>
        /// <param name="height">保存圖片的高度</param>
        /// <returns></returns>
        public Image ResizeImage(Image fromImage, int offsetX, int offsetY, int width, int height)
        {
            //創建新圖位圖
            Bitmap bitmap = new Bitmap(width, height);
            //創建作圖區域
            Graphics graphic = Graphics.FromImage(bitmap);
            //截取原圖相應區域寫入作圖區
            graphic.DrawImage(fromImage, 0, 0, new Rectangle(offsetX, offsetY, width, height), GraphicsUnit.Pixel);
            //從作圖區生成新圖
            Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
            ////保存圖片
            //saveImage.Save(toImagePath, ImageFormat.Png);
            ////釋放資源   
            //saveImage.Dispose();
            //graphic.Dispose();
            //bitmap.Dispose();
            return saveImage;
        }

        /// <summary> 
        /// 按照指定大小縮放圖片,但是為了保證圖片寬高比自動截取 
        /// </summary> 
        /// <param name="srcPath">原圖片路徑</param> 
        /// <param name="destWidth">目標圖片寬</param> 
        /// <param name="destHeight">目標圖片高</param> 
        /// <returns></returns> 
        public static Bitmap SizeImageWithOldPercent(Bitmap srcPath, int destWidth, int destHeight)
        {
            Image srcImage = srcPath;
            try
            {
                // 要截取圖片的寬度(原始寬度) 
                int srcWidth = srcImage.Width;
                // 要截取圖片的高度(原始高度) 
                int srcHeight = srcImage.Height;
                // 截取開始橫坐標
                int newX = 0;
                // 截取開始縱坐標 
                int newY = 0;

                // 截取比例 
                double whPercent = ((double)destWidth / (double)destHeight) * ((double)srcImage.Height / (double)srcImage.Width);
                if (whPercent > 1)
                {
                    // 當前圖片寬度對於要截取比例過大時 
                    srcWidth = int.Parse(Math.Round(srcImage.Width / whPercent).ToString());
                }
                else if (whPercent < 1)
                {
                    // 當前圖片高度對於要截取比例過大時 
                    srcHeight = int.Parse(Math.Round(srcImage.Height * whPercent).ToString());
                }

                if (srcWidth != srcImage.Width)
                {
                    // 寬度有變化時,調整開始截取的橫坐標 
                    newX = Math.Abs(int.Parse(Math.Round(((double)srcImage.Width - srcWidth) / 2).ToString()));
                }
                else if (srcHeight == srcImage.Height)
                {
                    // 高度有變化時,調整開始截取的縱坐標 
                    newY = Math.Abs(int.Parse(Math.Round(((double)srcImage.Height - (double)srcHeight) / 2).ToString()));
                }

                // 將原始圖片畫到目標畫布上,返回目標圖片
                Bitmap cutedImage = CutImage(srcImage, newX, newY, srcWidth, srcHeight);

                // 在創建一個新的畫布
                Bitmap bmp = new Bitmap(destWidth, destHeight);
                Graphics g = Graphics.FromImage(bmp);

                //設置高質量插值法
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                //設置高質量,低速度呈現平滑程度
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.Clear(Color.Transparent);

                //防止出現漸變
                var imgAtt = new ImageAttributes();
                imgAtt.SetWrapMode(WrapMode.TileFlipXY);

                // g.DrawImage(cutedImage, new Rectangle(0, 0, destWidth, destHeight), new Rectangle(0, 0, cutedImage.Width, cutedImage.Height), GraphicsUnit.Pixel);
                // 在畫板的指定位置畫圖
                g.DrawImage(cutedImage, new Rectangle(0, 0, destWidth, destHeight), 0, 0, cutedImage.Width, cutedImage.Height, GraphicsUnit.Pixel, imgAtt);
                // g.Dispose();

                return bmp;
            }
            catch (Exception)
            {
                return null;
            }
        }


        /// <summary> 
        /// 剪裁 -- 用GDI+ 
        /// </summary> 
        /// <param name="image">原始圖片</param> 
        /// <param name="StartX">開始坐標X</param> 
        /// <param name="StartY">開始坐標Y</param> 
        /// <param name="destWidth">目標圖片寬度</param> 
        /// <param name="destHeight">目標圖片高度高度</param> 
        /// <returns>剪裁後的Bitmap</returns> 
        public static Bitmap CutImage(Image image, int StartX, int StartY, int destWidth, int destHeight)
        {
            int srcWidth = image.Width;
            int srcHeight = image.Height;
            if (StartX >= srcWidth || StartY >= srcHeight)
            {
                // 開始截取坐標過大時,結束處理 
                return null;
            }

            if (StartX + destWidth > srcWidth)
            {
                // 寬度過大時只截取到最大大小 
                destWidth = srcWidth - StartX;
            }

            if (StartY + destHeight > srcHeight)
            {
                // 高度過大時只截取到最大大小 
                destHeight = srcHeight - StartY;
            }

            try
            {
                // 根據目標圖片的大小,實例化一個畫布
                Bitmap bmpOut = new Bitmap(destWidth, destHeight);
                // 實例化一個畫筆
                Graphics g = Graphics.FromImage(bmpOut);

                //設置高質量插值法
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                //設置高質量,低速度呈現平滑程度
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.Clear(Color.Transparent);

                // 將原始圖片畫到目標畫布上
                // g.DrawImage(image,new Rectangle(0, 0, destWidth, destHeight), new Rectangle(StartX, StartY, destWidth, destHeight), GraphicsUnit.Pixel);

                //防止出現漸變(生成高質量不模糊的圖片)
                var imgAtt = new ImageAttributes();
                imgAtt.SetWrapMode(WrapMode.TileFlipXY);
                // 在畫板的指定位置畫圖
                g.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), StartX, StartY, destWidth, destHeight, GraphicsUnit.Pixel, imgAtt);

                g.Dispose();

                return bmpOut;
            }
            catch
            {
                return null;
            }

        }
        public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)
        {
            System.Drawing.Image imgSource = b;
            System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
            int sW = 0, sH = 0;
            // 按比例縮放           
            int sWidth = imgSource.Width;
            int sHeight = imgSource.Height;
            if (sHeight > destHeight || sWidth > destWidth)
            {
                if ((sWidth * destHeight) > (sHeight * destWidth))
                {
                    sW = destWidth;
                    sH = (destWidth * sHeight) / sWidth;
                }
                else
                {
                    sH = destHeight;
                    sW = (sWidth * destHeight) / sHeight;
                }
            }
            else
            {
                return b;
                //sW = sWidth;
                //sH = sHeight;
            }

            Bitmap outBmp = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage(outBmp);
            //設置高質量插值法
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //設置高質量,低速度呈現平滑程度
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;

            g.Clear(Color.Transparent);

            //防止出現漸變
            var imgAtt = new ImageAttributes();
            imgAtt.SetWrapMode(WrapMode.TileFlipXY);
            //在畫板的指定位置畫圖
            g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0,
                imgSource.Width, imgSource.Height, GraphicsUnit.Pixel, imgAtt);

            g.Dispose();
            // 以下代碼為保存圖片時,設置壓縮質量     
            //EncoderParameters encoderParams = new EncoderParameters();
            //long[] quality = new long[1];
            //quality[0] = 100;
            //EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            //encoderParams.Param[0] = encoderParam;

            imgSource.Dispose();
            return outBmp;
        }

  

以下是圖片處理常用的一些方法;

C#常用的圖片處理方法-圖片剪切、圖片壓縮、多圖合並代碼