2012-07-12 97 views
1

当我尝试此代码,以调整图片的大小:C#一般性错误发生在GDI +保存图像

ConvertToBitmap(txtImage.Text); 

    private void ConvertToBitmap(string filename) 
    { 
     if (File.Exists(filename)) 
     { 
      var origImg = System.Drawing.Image.FromFile(filename); 
      var widthDivisor = (double)origImg.Width/(double)900; 
      var heightDivisor = (double)origImg.Height/(double)750; 
      int newWidth, newHeight; 

      if (widthDivisor < heightDivisor) 
      { 
       newWidth = (int)((double)origImg.Width/widthDivisor); 
       newHeight = (int)((double)origImg.Height/widthDivisor); 
      } 
      else 
      { 
       newWidth = (int)((double)origImg.Width/heightDivisor); 
       newHeight = (int)((double)origImg.Height/heightDivisor); 
      } 

      var newImg = new Bitmap(newWidth, newHeight); 
      Graphics g = Graphics.FromImage(newImg); 
      g.DrawImage(origImg, new Rectangle(0, 0, newWidth, newHeight)); 
      System.Drawing.Imaging.EncoderParameters encoderParameters = new System.Drawing.Imaging.EncoderParameters(1); 
      encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)350); 
      newImg.Save(fullpath, GetImageCodeInfo("image/jpeg"), encoderParameters); 
      g.Dispose(); 
     } 
    } 

    public static ImageCodecInfo GetImageCodeInfo(string mimeType) 
    { 
     ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders(); 
     foreach (ImageCodecInfo ici in info) 
      if (ici.MimeType.Equals(mimeType, StringComparison.OrdinalIgnoreCase)) 
       return ici; 
     return null; 
    } 

    private ImageCodecInfo GetEncoder(ImageFormat format) 
    { 

     ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); 

     foreach (ImageCodecInfo codec in codecs) 
     { 
      if (codec.FormatID == format.Guid) 
      { 
       return codec; 
      } 
     } 
     return null; 
    } 

它调整图像,但一些如何显示错误a generic error occurred in gdi+。 欢迎任何额外的努力。

+1

我把你的代码运行它,看看有什么错误时抛出,它跑了就好了我买了修改尺寸的输出中的图片是你的错误在你的代码的其他部分? – 2012-07-12 06:09:37

+0

其实我知道,代码是在我的电脑做工精细..但我设置这种代码在其他电脑是通过错误 – 2012-07-12 06:15:48

+0

时,图像的大小调整它保存在我的项目文件夹中。 @ArifEqbal – 2012-07-12 06:16:29

回答

1

给安全完全权限的文件夹,然后它的做工非常精细

+1

可以请你分享如何设置在这种情况下文件夹的完整权限?我面临同样的问题。谢谢 – Bitsian 2013-06-25 12:14:28

0

发生误差时请注意,私人内存使用情况。由于内存泄漏也可能发生此问题。

我看到的代码被处理的图形和这应适当设置使用后的其他图形对象。

运行该方法在一个循环中,以查看是否内存消耗急剧增加。如果是,那么存在内存泄漏,并且您必须深入挖掘以找出在使用后不会丢弃哪些对象。

相关问题