如何在存储到数据库之前减少图像的大小

本文关键字:图像 存储 数据库 | 更新日期: 2023-09-27 18:06:06

我有代码转换图像存储到数据库之前由于这个图像是存储的,但它的大小没有变化,所以我的数据库变得太大了帮助减小其大小

请,我的代码在我的第一个注释

如何在存储到数据库之前减少图像的大小

这是一个很好的调整图像大小的变体:

    /// <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