2016-11-23 138 views
0

我正在尝试调整图像大小,当前图像尺寸为(1800*1197)并且有file size = 305kb,当时我试图将尺寸减小到(1200*900)并将文件保存为size drastically increases to 1.75mb减少图像尺寸增加文件大小C#

我已经叫下面的方法resizeImage像:

Image MainImage = resizeImage(BaseImage, 1200, 900, false); 

我使用的功能来调整图片如下:

public static Bitmap resizeImage(Image imgToResize, int lnWidth, int lnHeight, bool _FixHeightWidth) 
    { 
     System.Drawing.Bitmap bmpOut = null; 
     Graphics g; 
     try 
     { 
      Bitmap loBMP = new Bitmap(imgToResize); 
      ImageFormat loFormat = loBMP.RawFormat; 

      decimal lnRatio; 
      int lnNewWidth = 0; 
      int lnNewHeight = 0; 

      //*** If the image is smaller than a thumbnail just return it 

      if (loBMP.Width < lnWidth && loBMP.Height < lnHeight && _FixHeightWidth == false) 
      { 
       bmpOut = new Bitmap(loBMP.Width, loBMP.Height); 
       g = Graphics.FromImage(bmpOut); 

       g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; 
       g.FillRectangle(Brushes.White, 0, 0, loBMP.Width, loBMP.Height); 
       g.DrawImage(loBMP, 0, 0, loBMP.Width, loBMP.Height); 
       loBMP.Dispose(); 
       return bmpOut; 
      } 

      if (loBMP.Width > loBMP.Height) 
      { 
       lnRatio = (decimal)lnWidth/loBMP.Width; 
       lnNewWidth = lnWidth; 
       decimal lnTemp = loBMP.Height * lnRatio; 
       lnNewHeight = (int)lnTemp; 
      } 
      else 
      { 
       lnRatio = (decimal)lnHeight/loBMP.Height; 
       lnNewHeight = lnHeight; 
       decimal lnTemp = loBMP.Width * lnRatio; 
       lnNewWidth = (int)lnTemp; 
      } 

      if (_FixHeightWidth == true) 
      { 
       lnNewHeight = lnHeight; 
       lnNewWidth = lnWidth; 
      } 

      bmpOut = new Bitmap(lnNewWidth, lnNewHeight); 
      g = Graphics.FromImage(bmpOut); 
      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; 
      g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight); 
      g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight); 
      loBMP.Dispose(); 
     } 
     catch 
     { 
      return null; 
     } 
     return bmpOut; 
    } 

这种转换成功后,我只需保存的文件,而是的减少,它增加了文件大小。所以我只想要文件大小应该保持相同(305kb)或相应地减少它。

注意:我只是想就如何处理这种情况提出建议,也有一些解释会很好,什么东西导致这个问题,如果还有其他解决方案。

样品图片:enter image description here

更新:文件格式是'.jpg'

MainImage.Save(Path.Combine(pathForSaving, fileName)); 
+1

什么文件格式是输入和输出图像*文件*? JPEG? BMP? PNG? –

+2

以什么格式保存文件? – Pikoh

+0

对不起,忘了提及:)'.jpg' –

回答

1

我supose您正在使用的文件的原始的imageformat写入新的图像...如果原始图像一个位图,结果格式将是,只能改变扩展名......你需要设置一个编码器。 此外,您可以尝试将IterpolationMode更改为中低质量或将文件格式更改为PNG格式,以便互联网使用更好...