Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Drawing.Imaging;
Namespace Saveimagehigh
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
private void Button1_Click (object sender, EventArgs e)
{
System.Drawing.Image Image = System.Drawing.Image.FromFile ("d:\\111.jpg");
Saveimageforspecifiedquality (Image, "d:\\222.jpg", imageformat.jpeg);
Image. Dispose ();
}
<summary>
Save the picture by the specified compression quality and format (Microsoft's Image.Save method saves the image compression quality to 75)
</summary>
<param name= "Sourceimage" > Image object of the picture you want to save </param>
<param name= "Savepath" > images to save the absolute path </param>
<param name= "Imagequalityvalue" > Picture to save the compression quality, the value of this parameter is an integer of 1 to 100, the larger the value, the better the quality of the Save </param>
<returns> save succeeded, return true; return false</returns>
public bool Saveimageforspecifiedquality (System.Drawing.Image sourceimage, string savepath, int imagequalityvalue, System.Drawing.Imaging.ImageFormat format)
{
The following code sets the compression quality when the picture is saved
EncoderParameters encoderparameters = new EncoderParameters ();
Encoderparameter encoderparameter = new Encoderparameter (System.Drawing.Imaging.Encoder.Quality, Imagequalityvalue) ;
Encoderparameters.param[0] = Encoderparameter;
Try
{
imagecodecinfo[] Imagecodecinfoarray = Imagecodecinfo.getimageencoders ();
ImageCodecInfo jpegimagecodecinfo = null;
for (int i = 0; i < imagecodecinfoarray.length; i++)
{
if (Imagecodecinfoarray[i]. Formatdescription.equals (format. ToString (). ToUpper ()))
{
Jpegimagecodecinfo = Imagecodecinfoarray[i];
Break
}
}
Sourceimage.save (Savepath, Jpegimagecodecinfo, encoderparameters);
return true;
}
Catch
{
return false;
}
}
}
}
Set compression quality when saving a picture