原图如下:
C# 图片压缩,压缩后尺寸为400*300像素,容量越小越好,请教,先谢谢各位了
public static System.Drawing.Image GetThumb(System.Drawing.Image sourceImg, int width, int height)
{
System.Drawing.Image targetImg = new Bitmap(width, height);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(targetImg))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
//g.Clear(Color.White);
g.DrawImage(sourceImg, new Rectangle(0, 0, width, height), new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), GraphicsUnit.Pixel);
g.Dispose();
}
return targetImg;
原图本来才140K的1240*1754的尺寸,我用以上代码压缩后居然还有114K,我的目标是越小越好,对质量没要求,因为要在网页里显示,图片太大了。请问有没有其他解决方法?
public class CompressPic { public void Save(string sourcefullname, int dispMaxWidth, int dispMaxHeight,string output) { Bitmap mg = new Bitmap(sourcefullname); Size newSize = new Size(dispMaxWidth, dispMaxHeight); Bitmap bp = ResizeImage(mg, newSize); if (bp != null) bp.Save(Path.Combine(output, Path.GetFileName(sourcefullname)), System.Drawing.Imaging.ImageFormat.Jpeg); } Bitmap ResizeImage(Bitmap mg, Size newSize) { double ratio = 0d; double myThumbWidth = 0d; double myThumbHeight = 0d; int x = 0; int y = 0; Bitmap bp; if ((mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height / Convert.ToDouble(newSize.Height))) ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width); else ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height); myThumbHeight = Math.Ceiling(mg.Height / ratio); myThumbWidth = Math.Ceiling(mg.Width / ratio); Size thumbSize = new Size((int)newSize.Width, (int)newSize.Height); bp = new Bitmap(newSize.Width, newSize.Height); x = (newSize.Width - thumbSize.Width) / 2; y = (newSize.Height - thumbSize.Height); System.Drawing.Graphics g = Graphics.FromImage(bp); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height); g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel); return bp; } }
FYI ,这个可以压出来有 16K
你好,是能压缩到16K,谢谢。问下,这代码怎么没看出区别呢?请指示下,O(∩_∩)O~
@jtans: 问题可能出现在你保存的时候 , 如果不设置保存格式 默认是PNG的
新人,请问怎么调用这个类?请指教学习中!
@backupbaby:
如
new CompressPic().Save(@"d:\mypic.jpg",200,400,@"d:\output")
/// <summary> /// jpeg图片压缩 /// </summary> /// <param name="sFile"></param> /// <param name="outPath"></param> /// <param name="flag"></param> /// <returns></returns> public static bool GetPicThumbnail(string sFile, string outPath, int flag) { System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile); ImageFormat tFormat = iSource.RawFormat; //以下代码为保存图片时,设置压缩质量 EncoderParameters ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = flag;//设置压缩的比例1-100 EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; try { 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; } } if (jpegICIinfo != null) { iSource.Save(outPath, jpegICIinfo, ep);//dFile是压缩后的新路径 } else { iSource.Save(outPath, tFormat); } return true; } catch { return false; } finally { iSource.Dispose(); iSource.Dispose(); } }
再给你一个图片质量压缩的方法
如果是要生成缩略图,可以参考 http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k(System.Drawing.Image.GetThumbnailImage);k(GetThumbnailImage);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-csharp)&rd=true