简体   繁体   English

如何压缩jpg图像?

[英]How to compress jpg image?

I have jpg image format with size of 3300K. 我有jpg图像格式,大小为3300K。

I try to compress the image => so i changed the image size but still i have very big size of the file ( ~ 800K ) 我尝试压缩图像=>所以我改变了图像大小,但我仍然有非常大的文件大小(~800K)

The change size method code: 更改大小方法代码:

    internal static Image resizeImage( Image imgToResize )
    {
        Bitmap b = new Bitmap( 300, 300 );

        using( Graphics g = Graphics.FromImage( ( Image )b ) )
        {
            //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.InterpolationMode = InterpolationMode.Low;

            g.DrawImage( imgToResize, 0, 0, 300, 300 );

            imgToResize.Dispose();
        }

        return ( Image )b;
    }

I must compress the image - even if the quality of the image will be less then the original. 我必须压缩图像 - 即使图像质量低于原始图像质量。

How can i do it ? 我该怎么做 ?

mg = image mg =图像

newsize = height and width newsize =身高和宽度

Call Resize function using following code 使用以下代码调用Resize功能

Bitmap mg = new Bitmap(strUploadPath);
Size newSize = new Size(Convert.ToInt32(DispMaxWidth), Convert.ToInt32(DispMaxHeight));
Bitmap bp = ResizeImage(mg, newSize);
if (bp != null)
bp.Save(strUploadPath, System.Drawing.Imaging.ImageFormat.Jpeg);

private Bitmap ResizeImage(Bitmap mg, Size newSize)
        {
            double ratio = 0d;
            double myThumbWidth = 0d;
            double myThumbHeight = 0d;
            int x = 0;
            int y = 0;

            Bitmap bp;

            if ((mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height /
            Convert.ToDouble(newSize.Height)))
                ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width);
            else
                ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height);
            myThumbHeight = Math.Ceiling(mg.Height / ratio);
            myThumbWidth = Math.Ceiling(mg.Width / ratio);

            //Size thumbSize = new Size((int)myThumbWidth, (int)myThumbHeight);
            Size thumbSize = new Size((int)newSize.Width, (int)newSize.Height);
            bp = new Bitmap(newSize.Width, newSize.Height);
            x = (newSize.Width - thumbSize.Width) / 2;
            y = (newSize.Height - thumbSize.Height);
            // Had to add System.Drawing class in front of Graphics ---
            System.Drawing.Graphics g = Graphics.FromImage(bp);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
            g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel);

            return bp;

        }

JPG images are already compressed. JPG图像已经过压缩。

You can try to decrease size of image using different algorithm of compression of decrease quality of image. 您可以尝试使用不同的压缩降低图像质量的算法来减小图像的大小。

In this case try to set of CompositingQuality or SmoothingMode properties to better (for you) values -- maybe there are others properties which can help. 在这种情况下,尝试将CompositingQualitySmoothingMode属性设置为更好(适合您)的值 - 也许还有其他属性可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM