如何在c#中存储到数据库之前减小图像大小

时间:2015-08-03 07:36:37

标签: c#

我有代码在存储到数据库之前转换图像 由于此图像是存储但其大小不变,因此我的数据库变得太大 帮助减小其大小

请将我的代码放在我的第一条评论中

1 个答案:

答案 0 :(得分:1)

以下是调整图片大小的好方法:

    /// <summary> 
    /// Reduce image by reducing quality
    /// </summary> 
    /// <param name="path"> Initially img. </param> 
    /// <param name="quality"> An integer from 0 to 100, with 100 being the highest quality. </param> 
    public static Image ReduceImg(Image img, int quality)
    {
        if (quality < 0 || quality > 100)
            throw new ArgumentOutOfRangeException("Quality is between 0 and 100");

        // Encoder parameter for image quality 
        EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);
        // JPEG image codec 
        ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;

        using (var stream = new MemoryStream())
        {
            img.Save(stream, jpegCodec, encoderParams);
            var b = new Bitmap(stream);
            return new Bitmap(b);
        }
    }

    /// <summary> 
    /// Returns the image codec with the given mime type 
    /// </summary> 
    private static ImageCodecInfo GetEncoderInfo(string mimeType)
    {
        // Get image codecs for all image formats 
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

        // Find the correct image codec 
        for (int i = 0; i < codecs.Length; i++)
            if (codecs[i].MimeType == mimeType)
                return codecs[i];

        return null;
    }

使用:

var yourNewImage = ReduceImg(yourImage, 50); // 50 for example. It's quality of new image