//将图片按百分比压缩,flag取值1到100,越小压缩比越大
public static bool YaSuo(Image iSource, string outPath, int flag)
{
ImageFormat tFormat = iSource.RawFormat;
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
if (jpegICIinfo != null)
iSource.Save(outPath, jpegICIinfo, ep);
else
iSource.Save(outPath, tFormat);
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
}
}