asp.net 图片压缩,压缩图片所占空间大小

#region 压缩图片

/// <summary>
/// 压缩图片
/// </summary>
/// <param name="filePath">要压缩的图片的路径</param>
/// <param name="filePath_ystp">压缩后的图片的路径</param>
public void ystp(string filePath, string filePath_ystp)
{
    //Bitmap
    Bitmap bmp = null;

    //ImageCoderInfo 
    ImageCodecInfo ici = null;

    //Encoder
    Encoder ecd = null;

    //EncoderParameter
    EncoderParameter ept = null;

    //EncoderParameters
    EncoderParameters eptS = null;

    try
    {
        bmp = new Bitmap(filePath);

        ici = this.getImageCoderInfo("image/jpeg");

        ecd = Encoder.Quality;

        eptS = new EncoderParameters(1);

        ept = new EncoderParameter(ecd, 10L);
        eptS.Param[0] = ept;
        bmp.Save(filePath_ystp, ici, eptS);
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
    finally
    {
        bmp.Dispose();

        ept.Dispose();

        eptS.Dispose();
    }
}

/// <summary>
/// 获取图片编码类型信息
/// </summary>
/// <param name="coderType">编码类型</param>
/// <returns>ImageCodecInfo</returns>
private ImageCodecInfo getImageCoderInfo(string coderType)
{
    ImageCodecInfo[] iciS = ImageCodecInfo.GetImageEncoders();

    ImageCodecInfo retIci = null;

    foreach (ImageCodecInfo ici in iciS)
    {
        if (ici.MimeType.Equals(coderType))
            retIci = ici;
    }

    return retIci;
}

#endregion 压缩图片


黑色头发:http://heisetoufa.iteye.com/

你可能感兴趣的:(C++,c,.net,asp.net,asp)