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.<>c__DisplayClassd.<InvokeActionMethodWithFilters>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.<>c__DisplayClass8.<BeginProcessRequest>b__4() +58
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
你的形象有多大? – Timwi 2010-09-09 03:34:49
它是2到3個大小。它所在的服務器有4個演出。 – 2010-09-09 03:43:02
這很奇怪。沒有任何東西可以讓我認爲存在內存泄漏。就調試而言,你是否嘗試過在沒有任何轉換的情況下繪製位圖?嘗試找到哪個屬性會導致異常縮小一點。泄漏是與這個函數分離的,還是在這個特定的調用中發生內存耗盡? – 2010-09-09 04:11:44