c# 無損高質量壓縮圖片代碼

最近,項目上涉及到了圖像壓縮,發現原有的圖像壓縮功能,雖然保證了圖像的大小300K之內,可是壓縮後的圖像看的不在清晰,而且,限定了圖片的Height或者是Width。算法

在CSDN上看到了一個壓縮算法:http://blog.csdn.net/qq_16542775/article/details/51792149測試

進過測試這個算法,發現,將原始圖像的大小進行對半處理,而後迭代跳轉壓縮質量參數,能夠獲得不錯的效果。spa

修改後的算法以下:.net

/// <summary>
/// 無損壓縮圖片
/// </summary>
/// <param name="sFile">原圖片地址</param>
/// <param name="dFile">壓縮後保存圖片地址</param>
/// <param name="flag">壓縮質量(數字越小壓縮率越高)1-100</param>
/// <param name="size">壓縮後圖片的最大大小</param>
/// <param name="sfsc">是不是第一次調用</param>
/// <returns></returns>
public static bool CompressImage(string sFile, string dFile, int flag = 90, int size = 300, bool sfsc = true)
{
//若是是第一次調用,原始圖像的大小小於要壓縮的大小,則直接複製文件,而且返回true
    FileInfo firstFileInfo = new FileInfo(sFile);
    if (sfsc == true && firstFileInfo.Length < size * 1024)
    {
        firstFileInfo.CopyTo(dFile);
        return true;
    }
Image iSource = Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;
int dHeight = iSource.Height / 2;
    int dWidth = iSource.Width / 2;
    int sW = 0, sH = 0;
    //按比例縮放
    Size tem_size = new Size(iSource.Width, iSource.Height);
    if (tem_size.Width > dHeight || tem_size.Width > dWidth)
    {
        if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
        {
            sW = dWidth;
            sH = (dWidth * tem_size.Height) / tem_size.Width;
        }
        else
        {
            sH = dHeight;
            sW = (tem_size.Width * dHeight) / tem_size.Height;
        }
    }
    else
    {
        sW = tem_size.Width;
        sH = tem_size.Height;
    }

    Bitmap ob = new Bitmap(dWidth, dHeight);
    Graphics g = Graphics.FromImage(ob);

    g.Clear(Color.WhiteSmoke);
    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    g.InterpolationMode = System.Drawing.Drawing2D.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是壓縮後的新路徑
            FileInfo fi = new FileInfo(dFile);
            if (fi.Length > 1024 * size)
            {
                flag = flag - 10;
                CompressImage(sFile, dFile, flag, size, false);
            }
        }
        else
        {
            ob.Save(dFile, tFormat);
        }
        return true;
    }
    catch
    {
        return false;
    }
    finally
    {
        iSource.Dispose();
        ob.Dispose();
    }
}

效果圖以下:code

第一張的大小是2.82M,尺寸是3680*4640。orm

第二張的大小是274KB,尺寸是1740*2320,清晰度方面仍是不錯的。blog

相關文章
相關標籤/搜索