private void button1_Click(object sender, EventArgs e) { Image myImage = Image.FromFile(@"D:picoriginal.jpg", true); SaveImg(@"D:pic educed.jpg", myImage, 50); } public static void SaveImg(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(System.Drawing.Imaging.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); img.Dispose(); } 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 < codecs.Length; i++) if (codecs[i].MimeType == mimeType) return codecs[i]; return null; }
测试结果:
https://stackoverflow.com/questions/1922040/how-to-resize-an-image-c-sharp/24199315#24199315