#region 以下代码为保存图片时,设置压缩质量
Image iSource = Image.FromStream(file.InputStream);//HttpPostedFile转Image long[] qy = new long[1]; qy[0] = 10;//设置压缩的比例1-100
ImageFormat tFormat = iSource.RawFormat;
int sW = iSource.Width, sH = iSource.Height;
Tool tool = new Tool();
tool.GetPicZoomSize(ref sW, ref sH, 640, 360);
Bitmap filemap = new Bitmap(640, 360);
Graphics g = Graphics.FromImage(filemap);
g.Clear(Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(iSource, new Rectangle((640 - sW) / 2, (360 - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
g.Dispose();
//Bitmap filemap = (Bitmap)Image.FromStream(file.InputStream);//HttpPostedFile转Bitmap
EncoderParameters ep = new EncoderParameters();
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
filemap.Save(FilePath + FileName + fileExt, jpegICIinfo, ep);//dFile是压缩后的新路径
filemap.Dispose();
#endregion
|
请发表评论