日期:2014-05-18  浏览次数:21217 次

怎么把2M或更大的图片的大小 变成 500K以内呢
上传的图片基本是都是很大的,可是别人要求的是500K 以内。。我把图片压缩成800×600的,可是图片还有1M多。。
郁闷,用程序该怎么来实现呢

------解决方案--------------------
C# code

任何压缩算法的压缩比都不能无限的提高的。
最有效地方法就是降低图片的分辨率,即像素值。图片的每个像素占用4个字节,假设一般JPG格式的压缩比为1:10的话,40K字节最多只能储存 40000/4*10 = 10万像素的图片,即图片的长乘以宽不超过10万,一般图片长宽比是4:3;所以图片的大小最好不超过 360 X 270 。
降低图片的分辨率就是缩小图片,用Graphics对象、Bitmap对象就能做到。

Bitmap bmpDest = new Bitmap(360,270);
Bitmap bmpSrc = new Bitmap("Source.jpg");
Graphics g = Graphics.FromImge(bmpDest);
g.DrawImage(bmpSrc, rectDest,rectSrc,GraphicsUnit.Pixel); 

或者将PictureBox的长和宽限定在360 X 270

------解决方案--------------------
有压缩算法的。我发给你参考下。
C# code

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace QQClass
{
    public class CompressImage
    {
        public void Compress_Image(string imageFile,string saveName)
        {
            Bitmap myBitmap;
            ImageCodecInfo myImageCodecInfo;
            Encoder myEncoder;
            EncoderParameter myEncoderParameter;
            EncoderParameters myEncoderParameters;
            // Create a Bitmap object based on a BMP file.
            myBitmap = new Bitmap(imageFile);
            // Get an ImageCodecInfo object that represents the JPEG codec.
            myImageCodecInfo = GetEncoderInfo("image/jpeg");
            // Create an Encoder object based on the GUID
            // for the Quality parameter category.
            myEncoder = Encoder.Quality;
            // Create an EncoderParameters object.
            // An EncoderParameters object has an array of EncoderParameter
            // objects. In this case, there is only one
            // EncoderParameter object in the array.
            myEncoderParameters = new EncoderParameters(1);
            // Save the bitmap as a JPEG file with quality level 25.
            myEncoderParameter = new EncoderParameter(myEncoder, 25L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            try
            {
                myBitmap.Save(saveName, myImageCodecInfo, myEncoderParameters);
            }
            catch
            { 
            
            }

            /*
             Save the bitmap as a JPEG file with quality level 50.
            myEncoderParameter = new EncoderParameter(myEncoder, 50L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            myBitmap.Save("Shapes050.jpg", myImageCodecInfo, myEncoderParameters);
            Save the bitmap as a JPEG file with quality level 75.
            myEncoderParameter = new EncoderParameter(myEncoder, 75L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            myBitmap.Save("Shapes075.jpg", myImageCodecInfo, myEncoderParameters);
            */
        }

        private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }
    }
}

------解决方案--------------------
选择jpg格式保存,可缩小的几百K以内,如果在jpg创建时使用了压缩比,则创建的jpg文件可在几十K以内。
------解决方案--------------------
private static ImageCodecInfo GetCodecInfo(string mimeType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ici in CodecInfo)
{
if (ici.MimeType == mimeType) return ici;
}
return null;
}

/**//// <summary>
/// 保存为JPEG格式,支持压缩质量选项
/// </summary>
public static bool SaveAsJPEG(Bitmap bmp, string FileName, int Qty)
{