必威体育Betway必威体育官网
当前位置:首页 > IT技术

c# 按质量压缩比压缩图片

时间:2019-08-07 06:15:23来源:IT技术作者:seo实验室小编阅读:89次「手机版」
 

压缩比

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

using System.Drawing.Imaging;

using System.IO;

namespace 图像压缩

{

public class image2

{

///

/// 按质量比和尺寸收缩呗数,生成缩略图,保存为另外路劲

///

/// 原始图片文件

/// 质量压缩比

/// 收缩倍数

/// 输出文件名

/// 成功返回true,失败则返回false

public bool getThumImage(String sourceFile, long quality, int multiple, String outputFile)

{

bool flag = false;

try

{

flag = ThumImage(sourceFile, quality, multiple, outputFile);

}

catch

{

flag = false;

}

return flag;

}

    /**/
    /// <summary>  
    /// 按质量比和尺寸收缩呗数,生成缩略图;保存在同一路径
    /// </summary>  
    /// <param name="sourceFile">原始图片文件</param>  
    /// <param name="quality">质量压缩比</param>  
    /// <param name="multiple">收缩倍数</param>  
    /// <param name="outputFile">输出文件名</param>  
    /// <returns>成功返回true,失败则返回false</returns>  
    public  bool getThumImage(String sourceFile, long quality, int multiple)
    {
        bool flag = false;
        try
        {
            flag = ThumImage(sourceFile, quality, multiple, sourceFile);
        }
        catch
        {
            flag = false;
        }
        return flag;
    }

    /// <summary>  
    /// 按图片尺寸大小压缩图片  ,保存在不同路径
    /// </summary>  
    /// <param name="sourceFile">原始图片文件</param>  
    /// <param name="multiple">收缩倍数</param>  
    /// <returns>成功返回true,失败则返回false</returns>  
    public  bool ImageByMultiple(String sourceFile, int multiple, String outputFile)
    {
        bool flag = false;
        try
        {
            flag = ThumImageByMultiple(sourceFile, multiple, outputFile);
        }
        catch
        {
            flag = false;
        }
        return flag;
    }


    /// <summary>  
    ///  按图片尺寸大小压缩图片  ,保存在同一路径
    /// </summary>  
    /// <param name="multiple">收缩倍数</param>  
    /// <returns>成功返回true,失败则返回false</returns>  
    public  bool ImageByMultiple(String sourceFile, int multiple)
    {
        bool flag = false;
        try
        {
            flag = ThumImageByMultiple(sourceFile, multiple, sourceFile);
        }
        catch
        {
            flag = false;
        }
        return flag;
    }


    /// <summary>  
    /// 质量压缩比,保存在不同一路径
    /// </summary>  
    /// <param name="sourceFile">原始图片文件</param>  
    /// <param name="quality">质量压缩比</param>  
    /// <returns>成功返回true,失败则返回false</returns>  
    public  bool ImageByQuality(String sourceFile, int quality, String outputFile)
    {
        bool flag = false;
        try
        {
            flag = ThumImageByQuality(sourceFile, quality, outputFile);
        }
        catch
        {
            flag = false;
        }
        return flag;
    }

    /// <summary>  
    /// 按质量压缩比压缩图片,保存在同一路径
    /// </summary>  
    /// <param name="sourceFile">原始图片文件</param>  
    /// <param name="quality">质量压缩比</param>  
    /// <returns>成功返回true,失败则返回false</returns>  
    public  bool ImageByQuality(String sourceFile, int quality)
    {
        bool flag = false;
        try
        {
            //如果图像小于300K,不进行压缩
            FileInfo firstFileInfo = new FileInfo(sourceFile);
            if (firstFileInfo.Length < 300 * 1024)
            {
                return flag;
            }
            flag = ThumImageByQuality(sourceFile, quality, sourceFile);
        }
        catch
        {
            flag = false;
        }
        return flag;
    }


    /// <summary>
    /// 按照图片质量生成图片,
    /// </summary>
    /// <param name="sourceFile">原始图片文件</param>  
    /// <param name="quality">质量压缩比</param>  
    /// <param name="outputFile">输出文件名</param>  
    /// <returns>成功返回true,失败则返回false</returns>  
    private  bool ThumImageByQuality(String sourceFile, long quality, String outputFile)
    {
        bool flag = false;
        try
        {
            long imageQuality = quality;
            Bitmap sourceImage = new Bitmap(sourceFile);

            //如果图片宽度小于2000,不进行压缩
            if (sourceImage.Width < 2000)
            {
                return flag;
            }

            ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
            System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
            Encoderparameters myEncoderParameters = new EncoderParameters(1);
            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);
            myEncoderParameters.Param[0] = myEncoderParameter;
            double xWidth = sourceImage.Width /3.5;
            double yWidth = sourceImage.Height / 3.5;
            Bitmap newImage = new Bitmap((int)(xWidth), (int)(yWidth));
            Graphics g = Graphics.FromImage(newImage);
            g.DrawImage(sourceImage, 0, 0, (int)xWidth, (int)yWidth);
            sourceImage.Dispose();
            g.Dispose();
            newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters);
            flag = true;
        }
        catch
        {
            flag = false;
        }
        return flag;

    }



    #region 调用图片处理具体方法

    /// <summary>
    /// 生成缩略图
    /// </summary>
    /// <param name="sourceFile">原始图片文件</param>  
    /// <param name="quality">质量压缩比</param>  
    /// <param name="multiple">收缩倍数</param>  
    /// <param name="outputFile">输出文件名</param>  
    /// <returns>成功返回true,失败则返回false</returns>  
    private  bool ThumImage(String sourceFile, long quality, int multiple, String outputFile)
    {
        bool flag = false;
        try
        {
            long imageQuality = quality;
            Bitmap sourceImage = new Bitmap(sourceFile);
            ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
            System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
            EncoderParameters myEncoderParameters = new EncoderParameters(1);
            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);
            myEncoderParameters.Param[0] = myEncoderParameter;
            float xWidth = sourceImage.Width;
            float yWidth = sourceImage.Height;
            Bitmap newImage = new Bitmap((int)(xWidth / multiple), (int)(yWidth / multiple));
            Graphics g = Graphics.FromImage(newImage);
            g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple);
            sourceImage.Dispose();
            g.Dispose();
            newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters);
            flag = true;
        }
        catch
        {
            flag = false;
        }
        return flag;

    }

    /// <summary>  
    /// 按图片尺寸大小压缩图片  
    /// </summary>  
    /// <param name="sourceFile">原始图片文件</param>  
    /// <param name="multiple">收缩倍数</param>  
    /// <param name="outputFile">输出文件名</param>  
    /// <returns>成功返回true,失败则返回false</returns>  
    public  bool ThumImageByMultiple(String sourceFile, int multiple, String outputFile)
    {
        try
        {
            Bitmap sourceImage = new Bitmap(sourceFile);
            ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
            float xWidth = sourceImage.Width;
            float yWidth = sourceImage.Height;
            Bitmap newImage = new Bitmap((int)(xWidth / multiple), (int)(yWidth / multiple));
            Graphics g = Graphics.FromImage(newImage);
            g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple);
            sourceImage.Dispose();
            g.Dispose();
            newImage.Save(outputFile);
            return true;
        }
        catch
        {
            return false;
        }
    }



    /// <summary>  
    /// 按图片尺寸大小压缩图片  
    /// </summary>  
    /// <param name="sourceFile">原始图片文件</param>  
    /// <param name="multiple">收缩倍数</param>  
    /// <param name="outputFile">输出文件名</param>  
    /// <returns>成功返回true,失败则返回false</returns>  
    public  bool FixSize(String sourceFile, int xWidth, int yWidth, String outputFile)
    {
        try
        {
            Bitmap sourceImage = new Bitmap(sourceFile);
            ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
            Bitmap newImage = new Bitmap((int)(xWidth), (int)(yWidth));
            Graphics g = Graphics.FromImage(newImage);
            g.DrawImage(sourceImage, 0, 0, xWidth, yWidth);
            sourceImage.Dispose();
            g.Dispose();
            newImage.Save(outputFile);
            return true;
        }
        catch
        {
            return false;
        }
    }
    #endregion getThumImage


    /// <summary>  
    /// 获取图片编码信息  
    /// </summary>  
    private static ImageCodecInfo GetEncoderInfo(String mimeType)
    {
        int j;
        ImageCodecInfo[] encoders;
        encoders = ImageCodecInfo.GetImageEncoders();
        for (j = 0; j < encoders.Length; ++j)
        {
            if (encoders[j].MimeType == mimeType)
                return encoders[j];
        }
        return null;
    }



}

}

相关阅读

C#编程规范

http://www.cnblogs.com/sntetwt/archive/2011/03/26/1996055.htmlC#编程规范第一章 概述. 5术语定义. 5Pascal 大小写. 5Camel

C# WinForm PropertyGrid用法

关于C# PropertyGrid的用法没有找到,找到一个C++的用法。模仿着使用了一下,感觉挺不错,分享一下。基本用法:拖个PropertyGrid,绑定一个

C# begininvoke (control)

namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() {

C#MD5加密算法的实例

using System;using System.Collections.Generic;using System.Text;using System.Security.Cryptography;namespace md5{ 

C#查询数据库--ExecuteReader方法的使用

在做数据库的查询过程中,使用方法ExecuteReader,其返回结果为MySqlDataReader,由于参考的信息有误,走了好长时间的弯路,记录下来; stri

分享到:

栏目导航

推荐阅读

热门阅读