欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#无损压缩图片

程序员文章站 2023-11-28 08:05:34
话不多说,请看代码: /// /// 根据指定尺寸得到按比例缩放的尺寸,返回true表示以更改尺寸 /// &...

话不多说,请看代码:

/// <summary>
    /// 根据指定尺寸得到按比例缩放的尺寸,返回true表示以更改尺寸
    /// </summary>
    /// <param name="picwidth">图片宽度</param>
    /// <param name="picheight">图片高度</param>
    /// <param name="specifiedwidth">指定宽度</param>
    /// /// <param name="specifiedheight">指定高度</param>
    /// <returns>返回true表示以更改尺寸</returns>
    private bool getpiczoomsize(ref int picwidth, ref int picheight, int specifiedwidth, int specifiedheight)
    {
      int sw = 0, sh = 0;
      boolean iszoomsize = false;
      //按比例缩放
      size tem_size = new size(picwidth, picheight);
      if (tem_size.width > specifiedwidth || tem_size.height > specifiedheight) //将**改成c#中的或者操作符号
      {
        if ((tem_size.width * specifiedheight) > (tem_size.height * specifiedwidth))
        {
          sw = specifiedwidth;
          sh = (specifiedwidth * tem_size.height) / tem_size.width;
        }
        else
        {
          sh = specifiedheight;
          sw = (tem_size.width * specifiedheight) / tem_size.height;
        }
        iszoomsize = true;
      }
      else
      {
        sw = tem_size.width;
        sh = tem_size.height;
      }
      picheight = sh;
      picwidth = sw;
      return iszoomsize;
    }
    /// <summary>
    /// 无损压缩图片
    /// </summary>
    /// <param name="sfile">原图片</param>
    /// <param name="dfile">压缩后保存位置</param>
    /// <param name="dheight">高度</param>
    /// <param name="dwidth">宽度</param>
    /// <param name="flag">压缩质量 1-100</param>
    /// <returns></returns>

    public bool getpicthumbnail(string sfile, string dfile, int dheight, int dwidth, int flag)
    {
      system.drawing.image isource = system.drawing.image.fromfile(sfile);
      imageformat tformat = isource.rawformat;
      int sw = isource.width, sh = isource.height;

      getpiczoomsize(ref sw, ref sh, dwidth, dheight);

      bitmap ob = new bitmap(dwidth, dheight);
      graphics g = graphics.fromimage(ob);
      g.clear(color.whitesmoke);
      g.compositingquality = compositingquality.highquality;
      g.smoothingmode = smoothingmode.highquality;
      g.interpolationmode = interpolationmode.highqualitybicubic;
      g.drawimage(isource, new rectangle((dwidth - sw) / 2, (dheight - sh) / 2, sw, sh), 0, 0, isource.width, isource.height, graphicsunit.pixel);
      g.dispose();
      //以下代码为保存图片时,设置压缩质量
      encoderparameters ep = new encoderparameters();
      long[] qy = new long[1];
      qy[0] = flag;//设置压缩的比例1-100
      encoderparameter eparam = new encoderparameter(system.drawing.imaging.encoder.quality, qy);
      ep.param[0] = eparam;
      try
      {
        imagecodecinfo[] arrayici = imagecodecinfo.getimageencoders();

        imagecodecinfo jpegiciinfo = null;

        for (int x = 0; x < arrayici.length; x++)
        {
          if (arrayici[x].formatdescription.equals("jpeg"))
          {
            jpegiciinfo = arrayici[x];
            break;
          }
        }
        if (jpegiciinfo != null)
        {
          ob.save(dfile, jpegiciinfo, ep);//dfile是压缩后的新路径
        }
        else
        {
          ob.save(dfile, tformat);
        }
        return true;
      }
      catch
      {
        return false;
      }
      finally
      {
        isource.dispose();
        ob.dispose();
      }
    }

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!