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

C# 在c语言中如何在存储到数据库之前减小图像的大小#,c#,C#,我有代码转换图像存储到数据库之前 由于此图像已存储,但其大小未更改,因此我的数据库变得太大 有助于缩小其尺寸 请将我的代码放在我的第一条评论中这里有一个调整图像大小的好方法: /// <summary> /// Reduce image by reducing quality /// </summary> /// <param name="path"> Initially img. </param> /

我有代码转换图像存储到数据库之前 由于此图像已存储,但其大小未更改,因此我的数据库变得太大 有助于缩小其尺寸


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

这里有一个调整图像大小的好方法:

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

我的代码是void convertImage(){if(image_pictureBox.image!=null){ms=new MemoryStream();image_pictureBox.image.Save(ms,ImageFormat.Jpeg);byte[]photo_array=new byte[ms.Length];ms.Position=0;ms.Read(photo_数组,0,photo_数组.Length);cmd.Parameters.AddWithValue(“@A_IMAGE”,photo_数组);}您是否需要更改大小和所有内容?使用问题按钮更新您的问题。请确切说明问题是什么,以及您迄今为止所做的尝试和修复工作。为什么不将图像存储在文件系统中并将图像路径存储在数据库中?是的……它的大小应该减小,因为我的数据库是如果我直接保存图像,会变得太重
var yourNewImage = ReduceImg(yourImage, 50); // 50 for example. It's quality of new image