本文介绍了Graphics.DrawImage:内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是不能为我的生命弄清楚为什么我在这里得到一个内存溢出异常,即使是在各种网站和论坛经过反复研究。是任何人都能够阐明这个代码的所有罪恶的根源一些轻?唯一的例外是在调用Graphics.DrawImage()方法,第79行抛出。
[HttpPost]
[ValidateAntiForgeryToken]
公众的ActionResult EditImage(FileModel模型)
{
变种FILEDATA =新MySite.Models.File(model.FileID,model.ClientID,ConfigurationManager.ConnectionStrings [mysite的。的ConnectionString)。数据;
图像的图像= NULL;
使用(VAR memStream =新的MemoryStream())
{
memStream.Write(FILEDATA,0,fileData.Length);
图像= Image.FromStream(memStream);
}使用
(VAR显卡= Graphics.FromImage(图片))
{
graphics.DrawImage(图像,model.x1,model.y1,(型号.x2 - model.x1),(model.y2 - model.y1));
graphics.Save();
}使用
(VAR memStream =新的MemoryStream())
{
image.Save(memStream,System.Drawing.Imaging.ImageFormat.Jpeg);
返回文件(memStream.ToArray(),图像/ JPEG);
}
}
堆栈跟踪:
[OutOfMemoryException异常:内存溢出]
System.Drawing.Graphics.CheckErrorStatus(的Int32状态)1143476
System.Drawing.Graphics .DrawImage(图像图像,的Int32 X,的Int32 Y,宽度的Int32,的Int32高)用C +141
ProPilot.Controllers.DocumentsController.EditImage(FileModel模型):\DEV\Web\Controllers\DocumentsController的.cs:79
lambda_method(封闭,ControllerBase,对象[])+104
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,对象[]参数)+14
系统。 Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary`2参数)211
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary`2参数)+27
系统.Web.Mvc.Async&所述;> c__DisplayClass42&所述。BeginInvokeSynchronousActionMethod方式> b__41()+28
System.Web.Mvc.Async&所述;方式> c__DisplayClass8`1&所述; BeginSynchronous> b__7(IAsyncResult的_ )+10
System.Web.Mvc.Async.WrappedAsyncResult`1.End()+57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult的asyncResult)+48
系统.Web.Mvc.Async&所述;方式> c__DisplayClass39&所述; BeginInvokeActionMethodWithFilters方式> b__33()+57
System.Web.Mvc.Async&所述;方式> c__DisplayClass4f&所述; InvokeActionMethodFilterAsynchronously> b__49()+223
System.Web.Mvc.Async&所述;方式> c__DisplayClass37&所述; BeginInvokeActionMethodWithFilters> b__36(IAsyncResult的asyncResult)+10
System.Web.Mvc.Async.WrappedAsyncResult`1.End()+57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult的asyncResult)+48
System.Web.Mvc.Async&所述;方式> c__DisplayClass2a&所述; BeginInvokeAction> b__20()+24
System.Web.Mvc.Async&所述;方式> c__DisplayClass25&所述; BeginInvokeAction> b__22(IAsyncResult的asyncResult)+102
System.Web.Mvc.Async.WrappedAsyncResult`1.End()+57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult的asyncResult)+43
System.Web.Mvc&所述;方式> c__DisplayClass1d&所述; BeginExecuteCore> b__18(IAsyncResult的asyncResult)+14
。系统.Web.Mvc.Async&所述;方式> c__DisplayClass4&所述; MakeVoidDelegate> b__3(IAsyncResult的AR)+23
System.Web.Mvc.Async.WrappedAsyncResult`1.End()+62
。系统.Web.Mvc.Controller.EndExecuteCore(IAsyncResult的asyncResult)+57
System.Web.Mvc.Async&所述;方式> c__DisplayClass4&所述; MakeVoidDelegate> b__3(IAsyncResult的AR)+23
的System.Web .Mvc.Async.WrappedAsyncResult`1.End()+62
System.Web.Mvc.Controller.EndExecute(IAsyncResult的asyncResult)+47
System.Web.Mvc.Controller.System.Web.Mvc .Async.IAsyncController.EndExecute(IAsyncResult的asyncResult)+10
System.Web.Mvc&所述;方式> c__DisplayClass8&所述; BeginProcessRequest> b__3(IAsyncResult的asyncResult)+25
System.Web.Mvc.Async <> c__DisplayClass4< MakeVoidDelegate> b__3(IAsyncResult的AR)+23
System.Web.Mvc.Async.WrappedAsyncResult`1.End()+62
System.Web.Mvc.MvcHandler .EndProcessRequest(IAsyncResult的asyncResult)+47
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult的结果)+9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep .Execute()9629296
System.Web.HttpApplication.ExecuteStep(IExecutionStep一步,布尔和放大器; completedSynchronously)+155
解决方案
@Sayse一针见血的。头以上他的意见
在使用Image.FromStream:
Source: http://msdn.microsoft.com/en-AU/library/93z9ee4x.aspx
using (var memStream = new MemoryStream())
{
memStream.Write(fileData, 0, fileData.Length);
using(Image image = Image.FromStream(memStream))
{
using (var graphics = Graphics.FromImage(image))
{
graphics.DrawImage(image, model.x1, model.y1, (model.x2 - model.x1), (model.y2 - model.y1));
graphics.Save();
}
using (var outStream = new MemoryStream())
{
image.Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg);
return File(outStream.ToArray(), "image/jpeg");
}
}
}
这篇关于Graphics.DrawImage:内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!