程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> asp.net 圖片質量壓縮(不改變尺寸),

asp.net 圖片質量壓縮(不改變尺寸),

編輯:C#入門知識

asp.net 圖片質量壓縮(不改變尺寸),



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;
}

/// <summary>
/// 圖片壓縮(降低質量以減小文件的大小)
/// </summary>
/// <param name="srcBitmap">傳入的Bitmap對象</param>
/// <param name="destStream">壓縮後的Stream對象</param>
/// <param name="level">壓縮等級,0到100,0 最差質量,100 最佳</param>
public static void Compress(Bitmap srcBitmap, Stream destStream, long level)
{
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;

// Get an ImageCodecInfo object that represents the JPEG codec.
myImageCodecInfo = GetEncoderInfo("image/jpeg");

// Create an Encoder object based on the GUID

// for the Quality parameter category.
myEncoder = Encoder.Quality;

// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one

// EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);

// Save the bitmap as a JPEG file with 給定的 quality level
myEncoderParameter = new EncoderParameter(myEncoder, level);
myEncoderParameters.Param[0] = myEncoderParameter;
srcBitmap.Save(destStream, myImageCodecInfo, myEncoderParameters);
}

/// <summary>
/// 圖片壓縮(降低質量以減小文件的大小)
/// </summary>
/// <param name="srcBitMap">傳入的Bitmap對象</param>
/// <param name="destFile">壓縮後的圖片保存路徑</param>
/// <param name="level">壓縮等級,0到100,0 最差質量,100 最佳</param>
public static void Compress(Bitmap srcBitMap, string destFile, long level)
{
Stream s = new FileStream(destFile, FileMode.Create);
Compress(srcBitMap, s, level);
s.Close();
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved