Asp.net中裁剪,缩小图片

private void Page_Load(object sender, System.EventArgs e)
          {
               ImgReduceCutOut(100,120,"1.jpg","2.jpg");
          }
          /// <summary>
          /// 缩小裁剪图片
          /// </summary>
          /// <param name="int_Width">要缩小裁剪图片宽度</param>
          /// <param name="int_Height">要缩小裁剪图片长度</param>
          /// <param name="input_ImgUrl">要处理图片路径</param>
          /// <param name="out_ImgUrl">处理完毕图片路径</param>
          public void ImgReduceCutOut(int int_Width,int int_Height,string input_ImgUrl,string out_ImgUrl)
          {
               // ===上传标准图大小===
               int int_Standard_Width=160;
               int int_Standard_Height=160;

               int Reduce_Width=0; // 缩小的宽度
               int Reduce_Height=0; // 缩小的高度
               int CutOut_Width=0; // 裁剪的宽度
               int CutOut_Height=0; // 裁剪的高度
               int level = 100; //缩略图的质量 1-100的范围
              
               // ===获得缩小,裁剪大小===
               if (int_Standard_Height*int_Width/int_Standard_Width>int_Height)
               {
                    Reduce_Width=int_Width;
                    Reduce_Height=int_Standard_Height*int_Width/int_Standard_Width;
                    CutOut_Width=int_Width;
                    CutOut_Height=int_Height;
               }
               else if (int_Standard_Height*int_Width/int_Standard_Width<int_Height)
               {
                    Reduce_Width=int_Standard_Width*int_Height/int_Standard_Height;
                    Reduce_Height=int_Height;
                    CutOut_Width=int_Width;
                    CutOut_Height=int_Height;
               }
               else
               {
                    Reduce_Width=int_Width;
                    Reduce_Height=int_Height;
                    CutOut_Width=int_Width;
                    CutOut_Height=int_Height;
               }

               // ===通过连接创建Image对象===
               System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(input_ImgUrl));

               // ===缩小图片===
               System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(Reduce_Width, Reduce_Height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
               Bitmap bm=new Bitmap(thumbnailImage);

               // ===处理JPG质量的函数===
               ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
               ImageCodecInfo ici=null;
               foreach(ImageCodecInfo codec in codecs)
               {
                    if(codec.MimeType=="image/jpeg")
                         ici=codec;
               }
               EncoderParameters ep=new EncoderParameters();
               ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)level);
              
               bm.Save(Server.MapPath("2.jpg"),ici,ep);

               // ===裁剪图片===
               Rectangle cloneRect = new Rectangle(0, 0, CutOut_Width, CutOut_Height);
               PixelFormat format = bm.PixelFormat;
               Bitmap cloneBitmap = bm.Clone(cloneRect, format);

               // ===保存图片===
               cloneBitmap.Save(Server.MapPath(out_ImgUrl),ici,ep);
          }

          public bool ThumbnailCallback()
          {
               return false;
          }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值