1. 程式人生 > >實現影象剪裁 jquery.Jcrop

實現影象剪裁 jquery.Jcrop

        /// <summary>
        /// 剪裁影象
        /// </summary>
        /// <param name="Img">原圖實體地址</param>
        /// <param name="Width">新圖寬度</param>
        /// <param name="Height">新圖高度</param>
        /// <param name="X">繪製起點X軸</param>
        /// <param name="Y">繪製起點Y軸</param>
        /// <returns></returns>
        private byte[] Crop(string Img, int Width, int Height, int X, int Y)
        {
            try
            {
                using (var OriginalImage = new Bitmap(Img))
                {
                    using (var bmp = new Bitmap(Width, Height, OriginalImage.PixelFormat))
                    {
                        bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
                        using (Graphics Graphic = Graphics.FromImage(bmp))
                        {
                            Graphic.SmoothingMode = SmoothingMode.AntiAlias;//設定高質量,低速度呈現平滑程度
                            Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;//設定高質量插值法
                            Graphic.Clear(Color.Transparent);//清空畫布並以透明背景色填充
                            Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                            Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, Width, Height), X, Y, Width, Height,
                                              GraphicsUnit.Pixel);
                          方法一
                            bmp.Save(Server.MapPath("上傳裁剪") + "new.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);//圖片另存
                          方法二  //轉換二進位制流
                            var ms = new MemoryStream();
                            bmp.Save(ms, OriginalImage.RawFormat);

                            return ms.GetBuffer();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                throw (Ex);
            }
        }