【问题标题】:How compress image without change the original height ,width of image in c#如何在不改变c#中图像的原始高度、宽度的情况下压缩图像
【发布时间】:2012-09-11 20:56:14
【问题描述】:

我想让图像大小小于其原始大小。我正在使用以下代码来压缩大小图像,但它将图像大小从 1MB 增加到 1.5MB
在不改变图像原始高度、宽度的情况下压缩大尺寸图像的任何其他解决方案。

    public static byte[] CompressImage(Image img) {

            int originalwidth = img.Width, originalheight = img.Height;

            Bitmap bmpimage = new Bitmap(originalwidth, originalheight);

            Graphics gf = Graphics.FromImage(bmpimage);
            gf.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            gf.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.AssumeLinear;
            gf.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

            Rectangle rect = new Rectangle(0, 0, originalwidth, originalheight);
            gf.DrawImage(img, rect, 0, 0, originalwidth, originalheight, GraphicsUnit.Pixel);

            byte[] imagearray;

            using (MemoryStream ms = new MemoryStream())
            {
                bmpimage.Save(ms, ImageFormat.Jpeg);
                imagearray= ms.ToArray();
            }

            return imagearray;
        }

【问题讨论】:

  • 你能改写你的问题吗?当您的意思是“缩小”时,听起来您正在使用“压缩”,同时您还在谈论 JPEG 文件格式的实际文件压缩问题。
  • 我想在不损失质量的情况下缩小图像尺寸。这是我的问题。

标签: c# .net image system.drawing


【解决方案1】:

您可以在将文件另存为 JPEG 时设置质量级别,这主要与文件大小直接相关 - 质量越低,输出文件越小。

另见How to: Set JPEG Compression Level,示例见this SO answer

【讨论】:

  • 我也试过这个并且工作,但是当我将它转换为字节数组以保存在数据库中时,它会抛出异常“不允许大于 max_allowed_pa​​cket 的数据包。”当我使用我的代码时,它工作正常。知道会是什么问题吗?
  • @AskQuestion:这听起来像是与您的位图问题无关的 MySql 错误 - 确保将压缩设置为实际 降低 输出文件大小的值 - 我相信默认质量为 50 - 但 RTFM :-)
  • 转换后我得到了这个异常,所以我问一下。你能告诉我它是什么类型的编码吗?我不知道这个函数:)
【解决方案2】:

正如@BrokenGlass 所说,您可以在EncoderParameter 中指定压缩级别。如果你想尝试改变质量,这里有一个 sn-p:

public static void SaveJpeg(string path, Image image, int quality)
{
    //ensure the quality is within the correct range
    if ((quality < 0) || (quality > 100))
    {
        //create the error message
        string error = string.Format("Jpeg image quality must be between 0 and 100, with 100 being the highest quality.  A value of {0} was specified.", quality);
        //throw a helpful exception
        throw new ArgumentOutOfRangeException(error);
    }

    //create an encoder parameter for the image quality
    EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
    //get the jpeg codec
    ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

    //create a collection of all parameters that we will pass to the encoder
    EncoderParameters encoderParams = new EncoderParameters(1);
    //set the quality parameter for the codec
    encoderParams.Param[0] = qualityParam;
    //save the image using the codec and the parameters
    image.Save(path, jpegCodec, encoderParams);
}

【讨论】:

  • 我也尝试过,但它会抛出异常“不允许大于 max_allowed_pa​​cket 的数据包。”
猜你喜欢
  • 2010-12-16
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 2011-08-09
  • 1970-01-01
  • 2021-12-27
相关资源
最近更新 更多