首页 新闻 会员 周边 捐助

C# 图片压缩,压缩后尺寸为400*300像素,容量越小越好,请教,先谢谢各位了

1
悬赏园豆:100 [待解决问题]

原图如下:

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,我的目标是越小越好,对质量没要求,因为要在网页里显示,图片太大了。请问有没有其他解决方法?

jtans的主页 jtans | 初学一级 | 园豆:102
提问于:2013-02-20 09:22
< >
分享
所有回答(3)
0
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

Yu | 园豆:12990 (专家六级) | 2013-02-20 09:45

你好,是能压缩到16K,谢谢。问下,这代码怎么没看出区别呢?请指示下,O(∩_∩)O~

支持(0) 反对(0) jtans | 园豆:102 (初学一级) | 2013-02-20 10:21

@jtans: 问题可能出现在你保存的时候 , 如果不设置保存格式 默认是PNG的

支持(0) 反对(0) Yu | 园豆:12990 (专家六级) | 2013-02-20 10:42

新人,请问怎么调用这个类?请指教学习中!

支持(0) 反对(0) backupbaby | 园豆:200 (初学一级) | 2013-08-30 10:08

@backupbaby: 

new CompressPic().Save(@"d:\mypic.jpg",200,400,@"d:\output")

支持(0) 反对(0) Yu | 园豆:12990 (专家六级) | 2013-08-30 10:38
0
/// <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();
            }
        }

再给你一个图片质量压缩的方法

猿人甲 | 园豆:208 (菜鸟二级) | 2013-02-22 12:04
0

如果是要生成缩略图,可以参考 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

陈希章 | 园豆:2538 (老鸟四级) | 2013-03-10 10:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册