📌  相关文章
📜  减小图像大小 C# 代码示例

📅  最后修改于: 2022-03-11 14:48:53.206000             🧑  作者: Mango

代码示例1
public static void SaveJpeg (string path, Image img, int quality) 
{ 
    if (quality<0  ||  quality>100) 
        throw new ArgumentOutOfRangeException("quality must be between 0 and 100."); 

     // Encoder parameter for image quality 
     EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality); 
     // JPEG image codec 
     ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg"); 
     EncoderParameters encoderParams = new EncoderParameters(1); 
     encoderParams.Param[0] = qualityParam; 
     img.Save(path, jpegCodec, encoderParams); 
} 

///  
/// Returns the image codec with the given mime type 
///  
private static ImageCodecInfo GetEncoderInfo(string mimeType) 
{ 
     // Get image codecs for all image formats 
     ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); 

     // Find the correct image codec 
     for(int i=0; i