2 个解决方案
#1
不懂,帮你顶
#2
Setting the compression level when saving JPEG images
Images are serialized by an encoder specially adapted for the image format. Certain encoders, such as the JPEG encoder, can be instructed to alter the method serialization by the use of encoder parameters which specify the characteristics of the data written to the file or stream. The EncoderParameter class provides encapsulation for these different settings and may be applied to the specific image encoder before an image is saved.
In the case of Jpeg images, you can write files with differing levels of compression by using the specialized Quality encoder and a suitable compression setting as shown in the code in the following listing.
//Load a bitmap from file
Bitmap bm=(Bitmap)Image.FromFile("mypic.jpg");
//Get the list of available encoders
ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
//find the encoder with the image/jpeg mime-type
ImageCodecInfo ici=null;
foreach(ImageCodecInfo codec in codecs)
{
if(codec.MimeType=="image/jpeg")
ici=codec;
}
//Create a collection of encoder parameters (we only need one in the collection)
EncoderParameters ep=new EncoderParameters();
//We'll save images with 25%, 50%, 75% and 100% quality as compared with the original
for(int x=25;x<101;x+=25)
{
//Create an encoder parameter for quality with an appropriate level setting
ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)x);
//Save the image with a filename that indicates the compression quality used
bm.Save("C:\\quality"+x.ToString()+".jpg",ici,ep);
}
Images are serialized by an encoder specially adapted for the image format. Certain encoders, such as the JPEG encoder, can be instructed to alter the method serialization by the use of encoder parameters which specify the characteristics of the data written to the file or stream. The EncoderParameter class provides encapsulation for these different settings and may be applied to the specific image encoder before an image is saved.
In the case of Jpeg images, you can write files with differing levels of compression by using the specialized Quality encoder and a suitable compression setting as shown in the code in the following listing.
//Load a bitmap from file
Bitmap bm=(Bitmap)Image.FromFile("mypic.jpg");
//Get the list of available encoders
ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
//find the encoder with the image/jpeg mime-type
ImageCodecInfo ici=null;
foreach(ImageCodecInfo codec in codecs)
{
if(codec.MimeType=="image/jpeg")
ici=codec;
}
//Create a collection of encoder parameters (we only need one in the collection)
EncoderParameters ep=new EncoderParameters();
//We'll save images with 25%, 50%, 75% and 100% quality as compared with the original
for(int x=25;x<101;x+=25)
{
//Create an encoder parameter for quality with an appropriate level setting
ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)x);
//Save the image with a filename that indicates the compression quality used
bm.Save("C:\\quality"+x.ToString()+".jpg",ici,ep);
}
#1
不懂,帮你顶
#2
Setting the compression level when saving JPEG images
Images are serialized by an encoder specially adapted for the image format. Certain encoders, such as the JPEG encoder, can be instructed to alter the method serialization by the use of encoder parameters which specify the characteristics of the data written to the file or stream. The EncoderParameter class provides encapsulation for these different settings and may be applied to the specific image encoder before an image is saved.
In the case of Jpeg images, you can write files with differing levels of compression by using the specialized Quality encoder and a suitable compression setting as shown in the code in the following listing.
//Load a bitmap from file
Bitmap bm=(Bitmap)Image.FromFile("mypic.jpg");
//Get the list of available encoders
ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
//find the encoder with the image/jpeg mime-type
ImageCodecInfo ici=null;
foreach(ImageCodecInfo codec in codecs)
{
if(codec.MimeType=="image/jpeg")
ici=codec;
}
//Create a collection of encoder parameters (we only need one in the collection)
EncoderParameters ep=new EncoderParameters();
//We'll save images with 25%, 50%, 75% and 100% quality as compared with the original
for(int x=25;x<101;x+=25)
{
//Create an encoder parameter for quality with an appropriate level setting
ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)x);
//Save the image with a filename that indicates the compression quality used
bm.Save("C:\\quality"+x.ToString()+".jpg",ici,ep);
}
Images are serialized by an encoder specially adapted for the image format. Certain encoders, such as the JPEG encoder, can be instructed to alter the method serialization by the use of encoder parameters which specify the characteristics of the data written to the file or stream. The EncoderParameter class provides encapsulation for these different settings and may be applied to the specific image encoder before an image is saved.
In the case of Jpeg images, you can write files with differing levels of compression by using the specialized Quality encoder and a suitable compression setting as shown in the code in the following listing.
//Load a bitmap from file
Bitmap bm=(Bitmap)Image.FromFile("mypic.jpg");
//Get the list of available encoders
ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
//find the encoder with the image/jpeg mime-type
ImageCodecInfo ici=null;
foreach(ImageCodecInfo codec in codecs)
{
if(codec.MimeType=="image/jpeg")
ici=codec;
}
//Create a collection of encoder parameters (we only need one in the collection)
EncoderParameters ep=new EncoderParameters();
//We'll save images with 25%, 50%, 75% and 100% quality as compared with the original
for(int x=25;x<101;x+=25)
{
//Create an encoder parameter for quality with an appropriate level setting
ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)x);
//Save the image with a filename that indicates the compression quality used
bm.Save("C:\\quality"+x.ToString()+".jpg",ici,ep);
}