减小尺寸图像而不增加尺寸.net

时间:2014-11-28 15:42:31

标签: c#

我试图减少图像尺寸,例如 100px X 100px 50px X 50px 。我尝试了一些代码,但它增加了图像的大小,例如 270kb 700kb 。有什么想法吗?

我尝试了两种方法并得到了相同的结果:

 public void ResizeImage()
    {
        Image img = Image.FromFile("~/Images/image.jpg");

        double imgHeight = img.Size.Height;
        double imgWidth = img.Size.Width;

        double x = 0.5;
        //New sizes
        int newWidth = Convert.ToInt32(imgWidth * x);
        int newHeight = Convert.ToInt32(imgHeight * x);

        Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
        Image myThumbnail = img.GetThumbnailImage(newWidth, newHeight, myCallback, IntPtr.Zero);

        //Save
        myThumbnail.Save("~/Images/image.jpg");

    }

    public bool ThumbnailCallback()
    {
        return false;
    }


    public void ResizeImage2()
    {
        Image img = Image.FromFile("~/Images/image.jpg");

        double imgHeight = img.Size.Height;
        double imgWidth = img.Size.Width;

        double x = 0.5;
        //Seteo nuevos tamaños
        int newWidth = Convert.ToInt32(imgWidth * x);
        int newHeight = Convert.ToInt32(imgHeight * x);


        Bitmap b = new Bitmap(newWidth, newHeight);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        g.DrawImage(img, 0, 0, newWidth, newHeight);
        g.Dispose();

        b.Save("~/Images/image.jpg");


    }

由于

3 个答案:

答案 0 :(得分:2)

你把它保存为bmp。设置压缩。 http://msdn.microsoft.com/ru-ru/library/9t4syfhh(v=vs.110).aspx 您也可以阅读有关压缩质量High quality JPEG compression with c#

的帖子

对于所有重载,请看这里 http://msdn.microsoft.com/ru-ru/library/System.Drawing.Image.Save(v=vs.110).aspx

只需压缩

bmp.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Jpeg);

使用质量设置保存

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); 
ImageCodecInfo ici = null; 

foreach (ImageCodecInfo codec in codecs)
{ 
    if (codec.MimeType == "image/jpeg") 
    ici = codec; 
} 

EncoderParameters ep = new EncoderParameters(); 
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100); 
bm.Save("C:\\quality" + x.ToString() + ".jpg", ici, ep);

答案 1 :(得分:0)

您正在保存图像而未指定格式,请尝试使用Bitmap.Save()的其他重载: http://msdn.microsoft.com/en-us/library/ms142147(v=vs.110).aspx

如:

b.Save("~/Images/image.jpg", ImageFormat.Jpeg);

对于图像大小调整,我通常使用以下辅助方法,它支持保持纵横比和填充。原始来源: https://stackoverflow.com/a/9301367/1373170

为了方便起见,我将其更改为使用流:

    public static void ResizeImage(Image image, Stream outputStream, int maximumWidth, int maximumHeight, bool enforceRatio, bool addPadding)
    {
        var canvasWidth = maximumWidth;
        var canvasHeight = maximumHeight;
        var newImageWidth = maximumWidth;
        var newImageHeight = maximumHeight;
        var xPosition = 0;
        var yPosition = 0;

        if (enforceRatio)
        {
            var ratioX = maximumWidth / (double)image.Width;
            var ratioY = maximumHeight / (double)image.Height;
            var ratio = ratioX < ratioY ? ratioX : ratioY;
            newImageHeight = (int)(image.Height * ratio);
            newImageWidth = (int)(image.Width * ratio);

            if (addPadding)
            {
                xPosition = (int)((maximumWidth - (image.Width * ratio)) / 2);
                yPosition = (int)((maximumHeight - (image.Height * ratio)) / 2);
            }
            else
            {
                canvasWidth = newImageWidth;
                canvasHeight = newImageHeight;
            }
        }

        var thumbnail = new Bitmap(canvasWidth, canvasHeight);
        var graphic = Graphics.FromImage(thumbnail);

        if (enforceRatio && addPadding)
        {
            graphic.Clear(Color.White);
        }

        graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphic.SmoothingMode = SmoothingMode.HighQuality;
        graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
        graphic.CompositingQuality = CompositingQuality.HighQuality;
        graphic.DrawImage(image, xPosition, yPosition, newImageWidth, newImageHeight);

        thumbnail.Save(outputStream, ImageFormat.Png);
    }

注意:此代码使用PNG编码,但可以轻松更改为可配置的参数。

答案 2 :(得分:0)

您只需将保存格式设置为jpeg并选择不同的质量级别。只需几次尝试,您就可以找到合适的品质!

代码示例(取自http://msdn.microsoft.com/it-it/library/ytz20d80(v=vs.110).aspx

// Save the bitmap as a JPEG file with quality level 25.
myEncoderParameter = new EncoderParameter(myEncoder, 25L);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters);