c# GDI+ Image.Save()或者Bitmap.Save()方法保存高质量图片

时间:2023-01-30 20:53:11

GDI+保存质量比较高的图片的方法,使用Image或者Bitmap的Save方法调节图像保存的质量。

Image.Save方法共有五种重 载 
其中 Save(String, ImageCodecInfo, EncoderParameters) Save(Stream, ImageCodecInfo,EncoderParameters) 这两种方法课以调节图像质量。

Save.(string ,ImageCodeInfo,EncoderParameters) 使用方法:

参数:

filename                类型:System.String        字符串,包含要将此 Image 保存到的文件的名称。

encoder                 类型:System.Drawing.Imaging.ImageCodecInfo         Image  ImageCodecInfo

encoderParams    类型:System.Drawing.Imaging.EncoderParameters            用于该 Image  EncoderParameters


步骤:

①获得编码器的函数,

    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;
}
    可以直接复制过去使用。
获取编码器
    ImageCodecInfo myImageCodecInfo;
    //获得JPEG格式的编码器
    myImageCodecInfo = GetEncoderInfo("image/jpeg");
设置图像质量
 
    System.Drawing.Imaging.Encoder myEncoder;
    EncoderParameter myEncoderParameter;
    EncoderParameters myEncoderParameters;
    // for the Quality parameter category.
    myEncoder = System.Drawing.Imaging.Encoder.Quality;
    // EncoderParameter object in the array.
    myEncoderParameters = new EncoderParameters(1);
    //设置质量 数字越大质量越好,但是到了一定程度质量就不会增加了,MSDN上没有给范围,只说是32为非负整数
    myEncoderParameter = new EncoderParameter(myEncoder, 100L);
    myEncoderParameters.Param[0] = myEncoderParameter;
④保存图像
    bitmap.Save(@"D:\Users\Administrator\Desktop\Piexl1.jpeg", myImageCodecInfo, myEncoderParameters);

            

MSDN上的给的例子:

using System;
using System.Drawing;
using System.Drawing.Imaging;
class Example_SetJPEGQuality
{
public static void Main()
{
Bitmap myBitmap;
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;

// Create a Bitmap object based on a BMP file.
myBitmap = new Bitmap("Shapes.bmp");

// 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 25.
myEncoderParameter = new EncoderParameter(myEncoder, 25L);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters);

// Save the bitmap as a JPEG file with quality level 50.
myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save("Shapes050.jpg", myImageCodecInfo, myEncoderParameters);

// Save the bitmap as a JPEG file with quality level 75.
myEncoderParameter = new EncoderParameter(myEncoder, 75L);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save("Shapes075.jpg", myImageCodecInfo, myEncoderParameters);
}
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;
}
}