当前位置: 移动技术网 > IT编程>开发语言>.net > .Net 实现图片缩略图上传通用方法

.Net 实现图片缩略图上传通用方法

2018年08月08日  | 移动技术网IT编程  | 我要评论

智取冰淇淋,红美商城,超级像素战士

日常开发中,经常碰到图片上传的需求,尤其在商城系统开发的时候,商品列表商品图片展示如果使用高清原图,由于高清原图比较大,加载原图时间会大大增加,直接导致系统性能底下,用户体验不好,并发量高的时候直接就挂掉了,这时候后台上传图片的时候,就必须将原高清图进行压缩,生成高质量缩略图,然后在商品列表读取缩略图可以大大减少加载时间,起到一个性能优化的作用,当然在商品详情的时候还是得用高清原图!

以下代码,可以在实际开发中使用将图片高质量压缩,话不多说,代码贴下:

/// <summary>
    /// 生成缩略图或质量压缩
    /// </summary>
    /// <param name="sourcepath">源图路径(物理路径)</param>
    /// <param name="targetpath">缩略图路径(物理路径)</param>
    /// <param name="width">缩略图宽度,如果宽度为0则不缩略</param>
    /// <param name="height">缩略图高度,如果高度为0则不缩略</param>
    /// <param name="mode">生成缩略图的方式,默认为空,为空则不缩略高宽[hw 指定高宽缩放(不变形);w 指定宽,高按比例;h 指定高,宽按比例;cut 指定高宽裁减(不变形)]</param> 
    /// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param>
    /// <param name="size">压缩后图片的最大大小,0为不限制大小</param>
    public static void makethumbnail(string sourcepath, string targetpath, int width = 0, int height = 0, string mode = "", int flag = 100, int size = 0)
    {
      image sourceimage = null;
      image bitmap = null;
      graphics g = null;
      encoderparameters ep = null;
      encoderparameter eparam = null;
      try
      {
        sourceimage = image.fromfile(sourcepath);
        int towidth = 0;
        if (width > 0)
        {
          towidth = width;
        }
        else
        {
          towidth = sourceimage.width;
        }
        int toheight = 0;
        if (height > 0)
        {
          toheight = height;
        }
        else
        {
          toheight = sourceimage.height;
        }
        int x = 0;
        int y = 0;
        int ow = sourceimage.width;
        int oh = sourceimage.height;
        if (width > 0 && height > 0 && !string.isnullorwhitespace(mode))
        {
          switch (mode.toupper())
          {
            case "hw"://指定高宽缩放(不变形)
              int tempheight = sourceimage.height * width / sourceimage.width;
              if (tempheight > height)
              {
                towidth = sourceimage.width * height / sourceimage.height;
              }
              else
              {
                toheight = sourceimage.height * width / sourceimage.width;
              }
              break;
            case "w"://指定宽,高按比例          
              toheight = sourceimage.height * width / sourceimage.width;
              break;
            case "h"://指定高,宽按比例
              towidth = sourceimage.width * height / sourceimage.height;
              break;
            case "cut"://指定高宽裁减(不变形)        
              if ((double)sourceimage.width / (double)sourceimage.height > (double)towidth / (double)toheight)
              {
                oh = sourceimage.height;
                ow = sourceimage.height * towidth / toheight;
                y = 0;
                x = (sourceimage.width - ow) / 2;
              }
              else
              {
                ow = sourceimage.width;
                oh = sourceimage.width * height / towidth;
                x = 0;
                y = (sourceimage.height - oh) / 2;
              }
              break;
          }
        }
        //新建一个bmp图片
        bitmap = new bitmap(towidth, toheight);
        //新建一个画板
        g = graphics.fromimage(bitmap);
        g.compositingquality = compositingquality.highquality;
        //设置高质量插值法
        g.interpolationmode = interpolationmode.highqualitybicubic;
        //设置高质量,低速度呈现平滑程度
        g.smoothingmode = smoothingmode.highquality;
        //清空画布并以透明背景色填充
        g.clear(color.transparent);
        //在指定位置并且按指定大小绘制原图片的指定部分
        g.drawimage(sourceimage, new rectangle(0, 0, towidth, toheight),
          new rectangle(x, y, ow, oh),
          graphicsunit.pixel);
        //以下代码为保存图片时,设置压缩质量
        ep = new encoderparameters();
        long[] qy = new long[1];
        qy[0] = flag;//设置压缩的比例1-100
        eparam = new encoderparameter(system.drawing.imaging.encoder.quality, qy);
        ep.param[0] = eparam;
        imagecodecinfo[] arrayici = imagecodecinfo.getimageencoders();//获取图像编码器的信息
        imagecodecinfo jpegiciinfo = null;
        for (int i = 0; i < arrayici.length; i++)
        {
          if (arrayici[i].formatdescription.equals("jpeg"))
          {
            jpegiciinfo = arrayici[i];
            break;
          }
        }
        if (jpegiciinfo != null)
        {
          bitmap.save(targetpath, jpegiciinfo, ep);
          fileinfo fitarget = new fileinfo(targetpath);
          if (size > 0 && fitarget.length > 1024 * size)
          {
            flag = flag - 10;
            makethumbnail(sourcepath, targetpath, width, height, mode, flag, size);
          }
        }
        else
        {
          //以jpg格式保存缩略图
          bitmap.save(targetpath, imageformat.jpeg);
        }
      }
      catch (system.exception ex)
      {
        throw ex;
      }
      finally
      {
        if (sourceimage != null)
        {
          sourceimage.dispose();
        }
        if (bitmap != null)
        {
          bitmap.dispose();
        }
        if (g != null)
        {
          g.dispose();
        }
        if (ep != null)
        {
          ep.dispose();
        }
        if (eparam != null)
        {
          eparam.dispose();
        }
      }
    }

总结

以上所述是小编给大家介绍的.net 实现图片缩略图上传通用方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网