2010-09-09 87 views
2

当我旋转我的服务器上的图像。它会导致内存不足异常。旋转的图像导致outofmemory异常

/// <summary> 
    /// Rotates the specified img. 
    /// </summary> 
    /// <param name="img">The img.</param> 
    /// <param name="rotationAngle">The rotation angle.</param> 
    /// <returns></returns> 
    public static Bitmap Rotate(Image img, float rotationAngle) 
    { 
     //create an empty Bitmap image 
     Bitmap bmp = new Bitmap(img.Width, img.Height); 

     //turn the Bitmap into a Graphics object 
     using (Graphics gfx = Graphics.FromImage(bmp)) 
     { 
      //now we set the rotation point to the center of our image 
      gfx.TranslateTransform((float) bmp.Width/2, (float) bmp.Height/2); 

      //now rotate the image 
      gfx.RotateTransform(rotationAngle); 

      gfx.TranslateTransform(-(float) bmp.Width/2, -(float) bmp.Height/2); 

      //set the InterpolationMode to HighQualityBicubic so to ensure a high 
      //quality image once it is transformed to the specified size 
      gfx.SmoothingMode = SmoothingMode.HighQuality; 
      gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      gfx.CompositingQuality = CompositingQuality.HighQuality; 
      gfx.PixelOffsetMode = PixelOffsetMode.HighQuality; 

      //now draw our new image onto the graphics object 
      gfx.DrawImage(img, new Point(0, 0)); 
     } 

     //return the image 
     return bmp; 
    } 

唯一的例外发生在这条线:gfx.DrawImage(img, new Point(0, 0)); 我四处搜寻互联网,发现很多人有类似的问题,但没有解决方案。

有没有解决方案?或者除了GDI +还有另一个库可以用来旋转我的图像吗?也许有什么在WPF中?

堆栈跟踪:

[OutOfMemoryException: Out of memory.] 
    System.Drawing.Graphics.CheckErrorStatus(Int32 status) +1588745 
    System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y) +227 
    System.Drawing.Graphics.DrawImage(Image image, Point point) +26 
    Chucksoft.Core.Drawing.ImageResizer.Rotate(Image img, Single rotationAngle) in C:\Projects\ChucksoftCore\Branches\2.5.0\Source\Chucksoft.Core\Drawing\ImageResizer.cs:245 
    Chucksoft.Core.Drawing.ImageResizer.Rotate(Byte[] b, Single angle, ImageFormat format) in C:\Projects\ChucksoftCore\Branches\2.5.0\Source\Chucksoft.Core\Drawing\ImageResizer.cs:22 
    TheMemorableMoments.Domain.Services.ImageService.Rotate(IEnumerable`1 media, User user, Single angle) in C:\Projects\TheMemorableMoments\Branches\2.1.0\Source\TheMemorableMoments\Domain\Services\ImageService.cs:34 
    TheMemorableMoments.Domain.Services.ImageService.RotateLeft(List`1 media, User user) in C:\Projects\TheMemorableMoments\Branches\2.1.0\Source\TheMemorableMoments\Domain\Services\ImageService.cs:69 
    TheMemorableMoments.UI.Controllers.User.PhotosController.Rotate(IMediaFiles media, Action`2 resizeMethod) in C:\Projects\TheMemorableMoments\Branches\2.1.0\Source\TheMemorableMoments.UI\Controllers\User\PhotosController.cs:408 
    TheMemorableMoments.UI.Controllers.User.PhotosController.RotateLeft(Media media) in C:\Projects\TheMemorableMoments\Branches\2.1.0\Source\TheMemorableMoments.UI\Controllers\User\PhotosController.cs:396 
    lambda_method(Closure , ControllerBase , Object[]) +127 
    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +258 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39 
    System.Web.Mvc.&lt;&gt;c__DisplayClassd.&lt;InvokeActionMethodWithFilters&gt;b__a() +125 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +640 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +312 
    System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +709 
    System.Web.Mvc.Controller.ExecuteCore() +162 
    System.Web.Mvc.&lt;&gt;c__DisplayClass8.&lt;BeginProcessRequest&gt;b__4() +58 
    System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +20 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) 
+1

你的形象有多大? – Timwi 2010-09-09 03:34:49

+0

它是2到3个大小。它所在的服务器有4个演出。 – 2010-09-09 03:43:02

+0

这很奇怪。没有任何东西可以让我认为存在内存泄漏。就调试而言,你是否尝试过在没有任何转换的情况下绘制位图?尝试找到哪个属性会导致异常缩小一点。泄漏是与这个函数分离的,还是在这个特定的调用中发生内存耗尽? – 2010-09-09 04:11:44

回答

0

我得到它的工作。我从未想出“内存不足”异常。相反,我使用了图像类的RotateFlip方法。查看RotateFlip代码,在Reflector中,直接调用本地库。这种方法的缺点是,你只能以90度的增量旋转。对我而言,它的效果非常好。