【问题标题】:why do I get this Out Of Memory Exception?为什么我会得到这个内存不足异常?
【发布时间】:2012-10-27 21:06:14
【问题描述】:

我有这个 C# 段代码:

  string fileName = Path.GetTempPath() + "\\temp.bmp";
        image.Save(fileName);
        bmp = (Bitmap)Image.FromFile(fileName);

在这一行: bmp = (Bitmap)Image.FromFile(fileName);

我得到了这个例外:

**System.OutOfMemoryException was unhandled
Message=Out of memory.
Source=System.Drawing
StackTrace:
   at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
   at System.Drawing.Image.FromFile(String filename)
   at WindowsFormsApplication1.ShowContoursForm..ctor(Templates samples, IImage image) in C:\Michael work\MyContourAnalysis\WindowsFormsApplication1\ShowContoursForm.cs:line 40
   at WindowsFormsApplication1.Form1.tsbDislpayContours_Click(Object sender, EventArgs e) in C:\Michael work\MyContourAnalysis\WindowsFormsApplication1\MainForm.cs:line 170
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at WindowsFormsApplication1.Program.Main() in C:\Michael work\MyContourAnalysis\WindowsFormsApplication1\Program.cs:line 18
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

内部异常: **

知道为什么会出现此异常以及如何修复它?

【问题讨论】:

  • temp.bmp 的大小是多少?
  • bmp 是我创建的一个空对象:Bitmap bmp;

标签: c# .net bitmap


【解决方案1】:

您的图片文件似乎存在格式问题。请参阅此处了解FromFile method 例外情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 2015-07-11
    • 1970-01-01
    相关资源
    最近更新 更多