C#中保存的JPG的质量

我制作了一个小型C#应用,以创建.jpg格式的图像。

pictureBox.Image.Save(name,ImageFormat.Jpeg);

图像创建成功。我输入了原始图片,并对其做了一些处理并保存。但是,此新图片的质量低于原始图片的质量。

有什么方法可以设置所需的质量?

回答:

下面的代码示例演示如何使用EncoderParameter构造函数创建EncoderParameter。要运行此示例,请粘贴代码并调用VaryQualityLevel方法。

本示例需要位于c:的名为TestPhoto.jpg的图像文件。

private void VaryQualityLevel()

{

// Get a bitmap.

Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");

ImageCodecInfo jgpEncoder = 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", jgpEncoder,

myEncoderParameters);

myEncoderParameter = new EncoderParameter(myEncoder, 100L);

myEncoderParameters.Param[0] = myEncoderParameter;

bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jgpEncoder,

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", jgpEncoder,

myEncoderParameters);

}

private ImageCodecInfo GetEncoder(ImageFormat format)

{

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

foreach (ImageCodecInfo codec in codecs)

{

if (codec.FormatID == format.Guid)

{

return codec;

}

}

return null;

}

参考:http :

//msdn.microsoft.com/en-

us/library/system.drawing.imaging.encoderparameter.aspx

以上是 C#中保存的JPG的质量 的全部内容, 来源链接: utcz.com/qa/417141.html

回到顶部