2010-09-09 11 views
2

Lorsque je fais pivoter une image sur mon serveur. Il provoque une exception de mémoire insuffisante.La rotation de l'image entraîne une exception de mémoire

/// <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; 
    } 

L'exception se produit sur cette ligne: gfx.DrawImage(img, new Point(0, 0)); J'ai une recherche autour de l'Internet et a trouvé un certain nombre de personnes ayant un problème similaire, mais pas de solution.

Existe-t-il une solution? Ou y at-il une autre bibliothèque que je peux utiliser en plus du GDI + pour faire pivoter mes images? Peut-être qu'il y a quelque chose dans WPF?

Stacktrace:

[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

Quelle est la taille de votre image? – Timwi

+0

C'est 2 à 3 megs. Le serveur sur lequel il est a 4 concerts. –

+0

C'est bizarre. Rien ne me dépasse pour penser qu'il y avait une fuite de mémoire. En ce qui concerne le débogage, avez-vous essayé de dessiner sur le bitmap sans aucune transformation? Essayez de trouver quelle propriété entraînerait une réduction de l'exception. La fuite est-elle isolée par rapport à cette fonction ou est-ce que la mémoire est insuffisante pour cet appel particulier? –

Répondre

0

Je l'ai travail. Je n'ai jamais compris l'exception "Out Of Memory". Au lieu de cela, j'ai utilisé la méthode RotateFlip suspendue de la classe Image. En regardant le code RotateFlip, dans Reflector, c'est un appel direct dans la bibliothèque native. L'inconvénient de cette méthode, c'est que vous pouvez seulement faire des rotations par incréments de 90 degrés. Pour moi, ça marche très bien.