操作說明:設定 JPEG 壓縮層級
當您將影像儲存至磁碟以減少檔案大小或改善其品質時,可能會想要修改影像的參數。 您可以修改其壓縮層級來調整 JPEG 影像的品質。 若要在儲存 JPEG 影像時指定壓縮等級,您必須建立 EncoderParameters 物件,並將它傳遞給 Image 類別的 Save 方法。 初始化 EncoderParameters 物件,使其具有由一個 EncoderParameter 組成的陣列。 當您建立 EncoderParameter 時,請指定 Quality 編碼器和所需的壓縮等級。
範例
下列範例程式碼會建立 EncoderParameter 物件,並儲存三個 JPEG 影像。 透過修改傳遞給 EncoderParameter 建構函式的 long
值,使用不同的品質等級來儲存每個 JPEG 影像。 品質層級 0 對應到最大壓縮,而品質層級 100 對應到最小壓縮。
private void VaryQualityLevel()
{
// Get a bitmap. The using statement ensures objects
// are automatically disposed from memory after use.
using (Bitmap bmp1 = new Bitmap(@"C:\TestPhoto.jpg"))
{
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.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.
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jpgEncoder, myEncoderParameters);
myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"C:\TestPhotoQualityHundred.jpg", jpgEncoder, myEncoderParameters);
// Save the bitmap as a JPG file with zero quality level compression.
myEncoderParameter = new EncoderParameter(myEncoder, 0L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"C:\TestPhotoQualityZero.jpg", jpgEncoder, myEncoderParameters);
}
}
Private Sub VaryQualityLevel()
' Get a bitmap. The Using statement ensures objects
' are automatically disposed from memory after use.
Using bmp1 As New Bitmap("C:\TestPhoto.jpg")
Dim jpgEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
' Create an Encoder object based on the GUID
' for the Quality parameter category.
Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.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.
Dim myEncoderParameters As New EncoderParameters(1)
Dim myEncoderParameter As New EncoderParameter(myEncoder, 50L)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("c:\TestPhotoQualityFifty.jpg", jpgEncoder, myEncoderParameters)
myEncoderParameter = New EncoderParameter(myEncoder, 100L)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("C:\TestPhotoQualityHundred.jpg", jpgEncoder, myEncoderParameters)
' Save the bitmap as a JPG file with zero quality level compression.
myEncoderParameter = New EncoderParameter(myEncoder, 0L)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("C:\TestPhotoQualityZero.jpg", jpgEncoder, myEncoderParameters)
End Using
End Sub
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
Private Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo
Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
Dim codec As ImageCodecInfo
For Each codec In codecs
If codec.FormatID = format.Guid Then
Return codec
End If
Next codec
Return Nothing
End Function
編譯程式碼
這個範例需要:
Windows Forms 應用程式。
PaintEventArgs,這是 PaintEventHandler 的參數。
名為
TestPhoto.jpg
且位在 c:\ 的影像檔。