zoukankan      html  css  js  c++  java
  • 实现图像剪裁 jquery.Jcrop

    
      配合 jquery.Jcrop 实现上传图片进行剪裁保存功能
      
        <script src="js/jquery.min.js"></script>
        <script src="js/jquery.Jcrop.js"></script>
        <link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />
        <script type="text/javascript">
            jQuery(function ($) {
                //target是<img>的id
                jQuery('#target').Jcrop({
                    onChange: showCoords,
                    onSelect: showCoords
                });
            });
    
            function showCoords(c) {
                jQuery('#x').val(c.x); //x起点坐标
                jQuery('#y').val(c.y);
                jQuery('#x2').val(c.x2);//x终点坐标
                jQuery('#y2').val(c.y2);
                jQuery('#w').val(c.w);
                jQuery('#h').val(c.h);
            };
        </script>

    后台代码
            /// <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);
                }
            }


        
  • 相关阅读:
    spring通过注解依赖注入和获取xml配置混合的方式
    Spring 3.0就这么简单读书笔记
    spring源码测试
    spring文档翻译
    敏感词过滤算法
    mongodb的地理空间索引如何在solr中体现
    mongodb的地理空间索引常见的问题
    mysql的distinct理解
    zookeeper管理solr的配置文件
    驾校理论考试
  • 原文地址:https://www.cnblogs.com/itrena/p/7434049.html
Copyright © 2011-2022 走看看