C# 圖片壓縮處理
阿新 • • 發佈:2019-02-05
今天老大讓做一個圖片壓縮功能,自己在網上找了一些解決方法,也同時對方法進行了提煉!
在下面的程式碼中會有一個欄位Encoder.Quality 欄位,這個對應的有個一個values是神奇的值
MSDN解釋是:Quality 類別將指定的影象壓縮級別。 在用於構造 EncoderParameter, ,質量類別的有用值的範圍是從 0 到 100。 指定的數字越,越高壓縮和影象質量也因此越低。 零將為您提供最低的影象質量和 100 最高。
當將一個引數傳遞給影象編碼器中時,將引數封裝在 EncoderParameter 物件。 域中的一個 EncoderParameter 物件是指定的引數的類別的 GUID。 使用的靜態欄位 Encoder 類來檢索 Encoder ,其中包含引數的所需類別。
內建的影象編碼器 GDI+ 接收屬於多個類別的引數。 下表列出了所有類別,每個類別關聯的 GUID。
裡面有6個方法,如下
方法作用 | 方法名 |
---|---|
按質量比和尺寸收縮唄數,生成縮圖,儲存為另外路勁 | getThumImage(String sourceFile, long quality, int multiple, String outputFile) |
按質量比和尺寸收縮唄數,生成縮圖;儲存在同一路徑 | getThumImage(String sourceFile, long quality, int multiple) |
按圖片尺寸大小壓縮圖片 ,儲存在不同路徑 | ImageByMultiple(String sourceFile, int multiple, String outputFile) |
按圖片尺寸大小壓縮圖片 ,儲存在同一路徑 | ImageByMultiple(String sourceFile, int multiple) |
質量壓縮比,儲存在不同一路徑 | ImageByQuality(String sourceFile, int quality, String outputFile) |
按質量壓縮比壓縮圖片,儲存在同一路徑 | ImageByQuality(String sourceFile, int quality) |
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetFileInfo(@"D:\tupian");
//string pathPerc = @"D:\Koala.jpg";
//string source = @"D:\Koala.jpg";
////if (!File.Exists(pathPerc))
////{
//// File.Create(pathPerc).Close();
////}
////else
////{
//// File.Delete(pathPerc);
//// File.Create(pathPerc).Close();
////}
//getThumImage(source, 18, 3, pathPerc);
}
private void GetFileInfo(string filePath)
{
DirectoryInfo directoryInfo = new DirectoryInfo(filePath);
try
{
foreach (DirectoryInfo childDirectoryInfo in directoryInfo.GetDirectories())
{
string name = childDirectoryInfo.Name.ToString();
GetFileInfo(filePath + "\\" + childDirectoryInfo.Name.ToString());
}
foreach (FileInfo fileInfo in directoryInfo.GetFiles())
{
//Length單位為bytes, 1k=1024b
if (fileInfo.Length > 400 * 1024)
{
// getThumImage(fileInfo.FullName, 100, 1, fileInfo.FullName);
// ImageByQuality(fileInfo.FullName, 18);
// ImageByMultiple(fileInfo.FullName,1);
FixSize(fileInfo.FullName, 200, 100, fileInfo.FullName);
}
}
}
catch
{
}
}
#region 呼叫圖片處理的方法
/// <summary>
/// 按質量比和尺寸收縮唄數,生成縮圖,儲存為另外路勁
/// </summary>
/// <param name="sourceFile">原始圖片檔案</param>
/// <param name="quality">質量壓縮比</param>
/// <param name="multiple">收縮倍數</param>
/// <param name="outputFile">輸出檔名</param>
/// <returns>成功返回true,失敗則返回false</returns>
public static 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 static 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 static 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 static 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 static 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 static bool ImageByQuality(String sourceFile, int quality)
{
bool flag = false;
try
{
flag = ThumImageByQuality(sourceFile, quality, sourceFile);
}
catch
{
flag = false;
}
return flag;
}
#endregion
#region 呼叫圖片處理具體方法
/// <summary>
/// 按照圖片質量生成圖片,
/// </summary>
/// <param name="sourceFile">原始圖片檔案</param>
/// <param name="quality">質量壓縮比</param>
/// <param name="outputFile">輸出檔名</param>
/// <returns>成功返回true,失敗則返回false</returns>
private static bool ThumImageByQuality(String sourceFile, long quality, 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), (int)(yWidth));
Graphics g = Graphics.FromImage(newImage);
g.DrawImage(sourceImage, 0, 0, xWidth, yWidth);
sourceImage.Dispose();
g.Dispose();
newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters);
flag = true;
}
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>
private static 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 static 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 static 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;
}
}