asp.net 裁剪图片

        /// <summary>
        /// 裁剪图片
        /// </summary>
        /// <param name="intWidth">要缩小裁剪图片宽度</param>
        /// <param name="intHeight">要缩小裁剪图片长度</param>
        /// <param name="strOldImgUrl">要处理图片路径</param>
        /// <param name="strNewImgUrl">处理完毕图片路径</param>
        public void CutGeneratedImage(int intStartX, int intStartY, int intWidth, int intHeight, string strOldImgUrl, string strNewImgUrl)
        {
            int level = 100; //缩略图的质量 1-100的范围
            //缩小图片
            using (Bitmap originBmp = new Bitmap(strOldImgUrl))
            {

                //上传标准图大小
                int w = 400;
                int h = (int)Math.Ceiling(originBmp.Height * decimal.Divide(400m, originBmp.Width));

                Bitmap resizedBmp = new Bitmap(w, h);
                Graphics g = Graphics.FromImage(resizedBmp);
                //设置高质量插值法  
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                //设置高质量,低速度呈现平滑程度  
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                //消除锯齿
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.DrawImage(originBmp, new Rectangle(0, 0, w, h), new Rectangle(0, 0, originBmp.Width, originBmp.Height), GraphicsUnit.Pixel);


                //处理JPG质量的函数
                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo ici = null;
                foreach (ImageCodecInfo codec in codecs)
                {
                    if (codec.MimeType == "image/jpeg")
                    {
                        ici = codec;
                        break;
                    }

                }
                EncoderParameters ep = new EncoderParameters();
                ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)level);


                //裁剪图片
                Rectangle cloneRect = new Rectangle(intStartX, intStartY, intWidth, intHeight);
                PixelFormat format = originBmp.PixelFormat;

                Bitmap cloneBitmap = resizedBmp.Clone(cloneRect, format);
                //保存图片
                cloneBitmap.Save(strNewImgUrl, ici, ep);

                g.Dispose();
                resizedBmp.Dispose();
                originBmp.Dispose();
            }
        }

 

建产对象:
     Bitmap bm = new Bitmap("c:/1.bmp");
缩放:
     Bitmap bm1 = new Bitmap(bm,width,height);
格式转换:
     bm.save("c:/1.jpg",ImageFromat.Jpeg);
     bm1.Save("c:/1.gif", ImageFormat.Gif);
剪切一个区域:
     //剪切大小
    int cutwidth;
      int cutheight;
     Graphics g;
     //以大小为剪切大小,像素格式为32位RGB创建一个位图对像
     Bitmap bm1 = new Bitmap(width,height,PixelFormat.Format32bppRgb) ;
    //定义一个区域
     Rectangle rg = new Rectangle(0,0,cutwidth,cutheight);
     //要绘制到的位图
     g = Graphics.FromImage(bm1);
     //将bm内rg所指定的区域绘制到bm1
     g.DrawImage(bm,rg)

Asp.net(C#)-放大缩小图片尺寸

 /// <summary>
    /// 放大缩小图片尺寸
    /// </summary>
    /// <param name="picPath"></param>
    /// <param name="reSizePicPath"></param>
    /// <param name="iSize"></param>
    /// <param name="format"></param>
    public void PicSized(string picPath, string reSizePicPath, int iSize, ImageFormat format)
    {
        Bitmap originBmp = new Bitmap(picPath);
        int w = originBmp.Width * iSize;
        int h = originBmp.Height * iSize;
        Bitmap resizedBmp = new Bitmap(w, h);
        Graphics g = Graphics.FromImage(resizedBmp);
        //设置高质量插值法  
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //设置高质量,低速度呈现平滑程度  
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        //消除锯齿
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.DrawImage(originBmp, new Rectangle(0, 0, w, h), new Rectangle(0, 0, originBmp.Width, originBmp.Height), GraphicsUnit.Pixel);
        resizedBmp.Save(reSizePicPath, format);
        g.Dispose();
        resizedBmp.Dispose();
        originBmp.Dispose();
    }

引用地址 https://www.cnblogs.com/qfb620/archive/2010/05/22/1741519.html

 

前台

      layui.use('upload', function () {
            var $ = layui.jquery
                , upload = layui.upload;
            var jcrop_api, boundx, boundy;

            function updatePreview(c) {

                if (parseInt(c.w) > 0) {
                    var rx = 120 / c.w;
                    var ry = 120 / c.h;

                    $("#imgPreview").css({
                        width: Math.round(rx * boundx) + "px",
                        height: Math.round(ry * boundy) + "px",
                        marginLeft: "-" + Math.round(rx * c.x) + "px",
                        marginTop: "-" + Math.round(ry * c.y) + "px"
                    });
                }
                $("#hidXone").val(c.x);
                $("#hidYone").val(c.y);
                $("#hidXtwo").val(c.hidXtwo);
                $("#hidYtwo").val(c.hidYtwo);
                $("#hidImgWidth").val(c.w);
                $("#hidImgHeight").val(c.h);
            };

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: '../ServiceData/SaveImgCrop.ashx?action=upload' //改成您自己的上传接口
                , accept: 'images' //图片
                , acceptMime: 'image/*'
                , done: function (res) {
                    //如果上传失败
                    if (res.status > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $("#cutimg").html("<img id=\"imgOriginal\" name=\"imgOriginal\" width=\"400\" /><div style=\"overflow: hidden; position:absolute;top:0px;right:-150px;\"><div style=\"width: 110px; height: 110px; overflow: hidden;border-radius:110px\"><img id=\"imgPreview\" /></div><br /><input type=\"button\" id=\"btnImgCut\" onclick=\"cutSaveImg()\" style=\" height:30px;line-height:27px \" value=\"裁剪并保存图片\" class=\"uploadify-button \" /></div>");
                    $("#cutimg img").each(function () { $(this).attr("src", res.message); });
                    $("#hidImgUrl").val(res.message);
                    $('#imgOriginal').Jcrop({
                        onChange: updatePreview,
                        onSelect: updatePreview,
                        aspectRatio: 1,
                        //maxSize: [120, 120],
                        setSelect: [0, 0, 120, 120]
                    }, function () {
                        var bounds = this.getBounds();
                        boundx = bounds[0];
                        boundy = bounds[1];
                        jcrop_api = this;
                    });
                }
                , error: function () {
                    //演示失败状态,并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
        });

 

posted @ 2021-04-14 17:01  DR19  阅读(141)  评论(0编辑  收藏  举报