下面的示例从 BMP 文件创建 Bitmap 对象。此代码将位图保存为 3 个 JPEG 文件,每个都有不同的质量级别。
[C#]
[C#]
1using System;
2using System.Drawing;
3using System.Drawing.Imaging;
4class Example_SetJPEGQuality
5{
6public static void Main()
7{
8Bitmap myBitmap;
9ImageCodecInfo myImageCodecInfo;
10Encoder myEncoder;
11EncoderParameter myEncoderParameter;
12EncoderParameters myEncoderParameters;
13// Create a Bitmap object based on a BMP file.
14myBitmap = new Bitmap("Shapes.bmp");
15// Get an ImageCodecInfo object that represents the JPEG codec.
16myImageCodecInfo = GetEncoderInfo("image/jpeg");
17// Create an Encoder object based on the GUID
18// for the Quality parameter category.
19myEncoder = Encoder.Quality;
20// Create an EncoderParameters object.
21// An EncoderParameters object has an array of EncoderParameter
22// objects. In this case, there is only one
23// EncoderParameter object in the array.
24myEncoderParameters = new EncoderParameters(1);
25// Save the bitmap as a JPEG file with quality level 25.
26myEncoderParameter = new EncoderParameter(myEncoder, 25L);
27myEncoderParameters.Param[0] = myEncoderParameter;
28myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters);
29// Save the bitmap as a JPEG file with quality level 50.
30myEncoderParameter = new EncoderParameter(myEncoder, 50L);
31myEncoderParameters.Param[0] = myEncoderParameter;
32myBitmap.Save("Shapes050.jpg", myImageCodecInfo, myEncoderParameters);
33// Save the bitmap as a JPEG file with quality level 75.
34myEncoderParameter = new EncoderParameter(myEncoder, 75L);
35myEncoderParameters.Param[0] = myEncoderParameter;
36myBitmap.Save("Shapes075.jpg", myImageCodecInfo, myEncoderParameters);
37}
38private static ImageCodecInfo GetEncoderInfo(String mimeType)
39{
40int j;
41ImageCodecInfo[] encoders;
42encoders = ImageCodecInfo.GetImageEncoders();
43for(j = 0; j < encoders.Length; ++j)
44{
45if(encoders[j].MimeType == mimeType)
46return encoders[j];
47}
48return null;
49}
50}
51
52
2using System.Drawing;
3using System.Drawing.Imaging;
4class Example_SetJPEGQuality
5{
6public static void Main()
7{
8Bitmap myBitmap;
9ImageCodecInfo myImageCodecInfo;
10Encoder myEncoder;
11EncoderParameter myEncoderParameter;
12EncoderParameters myEncoderParameters;
13// Create a Bitmap object based on a BMP file.
14myBitmap = new Bitmap("Shapes.bmp");
15// Get an ImageCodecInfo object that represents the JPEG codec.
16myImageCodecInfo = GetEncoderInfo("image/jpeg");
17// Create an Encoder object based on the GUID
18// for the Quality parameter category.
19myEncoder = Encoder.Quality;
20// Create an EncoderParameters object.
21// An EncoderParameters object has an array of EncoderParameter
22// objects. In this case, there is only one
23// EncoderParameter object in the array.
24myEncoderParameters = new EncoderParameters(1);
25// Save the bitmap as a JPEG file with quality level 25.
26myEncoderParameter = new EncoderParameter(myEncoder, 25L);
27myEncoderParameters.Param[0] = myEncoderParameter;
28myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters);
29// Save the bitmap as a JPEG file with quality level 50.
30myEncoderParameter = new EncoderParameter(myEncoder, 50L);
31myEncoderParameters.Param[0] = myEncoderParameter;
32myBitmap.Save("Shapes050.jpg", myImageCodecInfo, myEncoderParameters);
33// Save the bitmap as a JPEG file with quality level 75.
34myEncoderParameter = new EncoderParameter(myEncoder, 75L);
35myEncoderParameters.Param[0] = myEncoderParameter;
36myBitmap.Save("Shapes075.jpg", myImageCodecInfo, myEncoderParameters);
37}
38private static ImageCodecInfo GetEncoderInfo(String mimeType)
39{
40int j;
41ImageCodecInfo[] encoders;
42encoders = ImageCodecInfo.GetImageEncoders();
43for(j = 0; j < encoders.Length; ++j)
44{
45if(encoders[j].MimeType == mimeType)
46return encoders[j];
47}
48return null;
49}
50}
51
52
来源:https://www.cnblogs.com/niuniu502/archive/2006/09/14/503812.html