本人跪求 图片压缩的方法!要源码!C#的最好!
本人最近在在做一个图片压缩的方法,我通过扫描仪扫描出来的图片有400K 
扫描出来的图片可能有彩色或灰度的,我想要的效果就是不改图片的色彩和宽、高,进行压缩!
我看了一个叫“LEAD OLE Server”的压缩软件,它在保存的时候,如下图:
本来1M的图片,保存后就变120K左右了!要的就是这种效果!

解决方案 »

  1.   


     /**//// <summary>
            /// 保存为JPEG格式,支持压缩质量选项
            /// </summary>
            /// <param name="bmp"></param>
            /// <param name="FileName"></param>
            /// <param name="Qty"></param>
            /// <returns></returns>
            public static bool KiSaveAsJPEG(Bitmap bmp, string FileName, int Qty)
            {
                try
                {
                    EncoderParameter p;
                    EncoderParameters ps;                ps = new EncoderParameters(1);                p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Qty);
                    ps.Param[0] = p;                bmp.Save(FileName, GetCodecInfo("image/jpeg"), ps);
                    
                    return true;
                }
                catch
                {
                    return false;
                }        }
      

  2.   

    google下7-zip 论坛里有现成的公开算法,直接拿来用就行了.
      

  3.   

    如果是保存数据的话,可以这样:/// <summary>   
            /// 压缩数据   
            /// </summary>   
            /// <param name="bySrc"></param>   
            /// <returns></returns>   
            public static bool GZIPCompress(ref byte[] bySrc)
            {
                MemoryStream ms = new MemoryStream();
                GZipStream gzipStream = new GZipStream(ms, CompressionMode.Compress, true);
                gzipStream.Write(bySrc, 0, bySrc.Length);
                gzipStream.Close();
                ms.Position = 0;
                bySrc = new byte[ms.Length];
                ms.Read(bySrc, 0, bySrc.Length);
                ms.Close();
                ms.Dispose();
                gzipStream.Dispose();
                return true;
            }        /// <summary>   
            /// 解压   
            /// </summary>   
            /// <param name="bySrc"></param>   
            /// <returns></returns>   
            public static bool GZIPDecompress(ref byte[] bySrc)
            {
                MemoryStream ms = new MemoryStream();
                ms.Write(bySrc, 0, bySrc.Length);
                ms.Seek(0, SeekOrigin.Begin);
                GZipStream gzipStream = new GZipStream(ms, CompressionMode.Decompress);
                //开始解压数据   
                int totallen = 0;
                List<byte[]> LstBuffer = new List<byte[]>();
                while(true)
                {
                    byte[] buffer = new byte[bySrc.Length];
                    int readlen = gzipStream.Read(buffer, 0, buffer.Length);
                    if(readlen == 0)
                    {
                        break;
                    }
                    totallen += readlen;
                    LstBuffer.Add(buffer);
                }
                //把解压的数据串在一起
                bySrc = new byte[totallen];
                for(int i = 0; i < LstBuffer.Count; i++)
                {
                    if(LstBuffer.Count - 1 == i)
                    {
                        Array.Copy(LstBuffer[i], 0, bySrc, LstBuffer[i].Length * i, totallen - LstBuffer[i].Length * i);
                    }
                    else
                    {
                        Array.Copy(LstBuffer[i], 0, bySrc, LstBuffer[i].Length * i, LstBuffer[i].Length);
                    }
                }
                ms.Close();
                ms.Dispose();
                gzipStream.Close();
                gzipStream.Dispose();
                return true;
            }
      

  4.   

    不考虑质量的话,你转换格式就可以了。1楼的方法是有损压缩,你如果不介意,可以转成gif格式,8位色,会更小。
    如果只自己软件用,可以考虑吧图片压缩为压缩包。
    http://topic.csdn.net/t/20040630/10/3133568.html
      

  5.   

    首先图片的压缩空间很小。
    其次如果压缩了,那就一定是有损压缩(在格式不变的前提下)。
    再次,使用jpg可以在保证一定质量的前提下,减小文件大小。
    就是1楼的方法。
      

  6.   

    哥们儿,这个我自己用过的很好用,记得添加命名空间using System.Drawing;pic_zero("NET/zp/0003628947.jpg", "NET/shangchuan/0003628947.jpg", 600);
    ///注意如果图片的高和宽都不到这个要求的600会报错,要判断他的宽或者高决不能小于这个600
    /// <param name="sourcepath">原路经</param>
        /// <param name="aimpath">保存路径</param>
        /// <param name="scale">所需高度或宽度</param>
        public void pic_zero(string sourcepath, string aimpath, int scale)//,,,,,,//////////////要试一下
        {
            string originalFilename = sourcepath;
            //生成的高质量图片名称   
            string strGoodFile = aimpath;        //从文件取得图片对象   
            System.Drawing.Image image = System.Drawing.Image.FromFile(originalFilename);
            int iImgWidth = image.Width;
            int iImgHeight = image.Height;
            int iScale = (iImgWidth / scale) > (iImgHeight / scale) ? (iImgWidth / scale) : (iImgHeight / scale);////C语言里的语法:条件表达式e1?e2:e3若e1为真(非0),则此表达式的值为e2的值;若为假,则表达式的值为e3        //取得图片大小   
            System.Drawing.Size size = new Size(image.Width / iScale, image.Height / iScale);
            //新建一个bmp图片   
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
            //新建一个画板   
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //设置高质量插值法   
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度   
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空一下画布   
            g.Clear(Color.Blue);
            //在指定位置画图   
            g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
            new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
            System.Drawing.GraphicsUnit.Pixel);
            //保存高清晰度的缩略图   
            bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);
            g.Dispose();
        }
      

  7.   

    // 方法1,按大小
        /// <summary>
        /// 按大小缩放图片
        /// </summary>
        /// <param name="Width">缩放到的宽</param>
        /// <param name="Height">缩放到的高</param>
        /// <param name="targetFilePath">图片的名字</param>
        /// <returns>bool</returns>
        public bool ReducedImage(int Width, int Height, string targetFilePath)
        {
            try
            {
                Image ReducedImage;
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
                ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
                ReducedImage.Dispose();
                return true;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
                return false;
            }
        }
        // 方法2,按百分比 缩小60% Percent为0.6 targetFilePath为目标路径
        /// <summary>
        /// 按百分比缩放
        /// </summary>
        /// <param name="Percent">小数:0.4表示百分之40</param>
        /// <param name="targetFilePath">图片的名称</param>
        /// <returns>bool</returns>
        public bool ReducedImage(double Percent, string targetFilePath)
        {
            try
            {
                Image ReducedImage;
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
                ImageHeight = (ResourceImage.Height)*ImageWidth/ ResourceImage.Width;//等比例缩放
                ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
                ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
                ReducedImage.Dispose();
                return true;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
                return false;
            }
        }
      

  8.   

    http://www.diyxm.com/html/article_30.html  
    http://hi.baidu.com/lmydesj/blog/item/78c2e93957c754cdd46225f0.html\
    楼主去看看吧