在进行更多测试后,我发现这个问题可能是由于图像没有被及时加载到位以便克隆成位图并显示。这种情况是否可能发生呢?
注意:是的,有其他问题的标题中会出现这个错误,但经过一番调查发现该错误是一个含糊不清的错误,有很多可能的原因。我没有找到与我的场景相同的问题。
我遇到了以下错误。
具体来说,第四行:
简化的目标是从精灵集中返回包含精灵的位图,基于精灵集索引和精灵索引。该位图在PictureBox中显示,直到它被更改为不同的图像。我知道逻辑是正确的,问题不在于此。我使用的.png大小为384x256。
所有参数都设置正确,所有引用的文件都在那里,一切似乎都按顺序进行。最奇怪的是有时它起作用,有时它不起作用。这让我相信它可能是System.Drawing本身内存泄漏,但我似乎无法追踪它。
编辑:更新了代码并添加了StackTrace。尽管在不再使用Bitmap时处理它们(请参阅下面的代码示例以了解如何处置Bitmap),但仍然存在相同的问题。
注意:是的,有其他问题的标题中会出现这个错误,但经过一番调查发现该错误是一个含糊不清的错误,有很多可能的原因。我没有找到与我的场景相同的问题。
我遇到了以下错误。
System.ArgumentException was unhandled
HResult=-2147024809
Message=Parameter is not valid.
Source=System.Drawing
这是由代码引起的。看似是随机的(即有时它能正常工作,有时则不能。每次在未重新启动VS和重建项目的情况下连续运行次数越多,它就越可能失败):
private Bitmap GetSprite(bool anim, int tsIndex, int tileIdx) {
System.Drawing.Rectangle cloneRect;
string prefix = (anim) ? "A" : "S";
using (Bitmap b = new Bitmap(prefix + tsIndex.ToString() + ".png")) {
if (anim) {
cloneRect = new System.Drawing.Rectangle(BaseObjects.A_AnimSpriteSets[tsIndex].StaticRecs[tileIdx].X, BaseObjects.A_AnimSpriteSets[tsIndex].StaticRecs[tileIdx].Y, BaseObjects.A_AnimSpriteSets[tsIndex].RecWidth, BaseObjects.A_AnimSpriteSets[tsIndex].RecHeight);
} else {
cloneRect = new System.Drawing.Rectangle(BaseObjects.A_StaticSpriteSets[tsIndex].StaticRecs[tileIdx].X, BaseObjects.A_StaticSpriteSets[tsIndex].StaticRecs[tileIdx].Y, BaseObjects.A_StaticSpriteSets[tsIndex].RecWidth, BaseObjects.A_StaticSpriteSets[tsIndex].RecHeight);
}
return b.Clone(cloneRect, b.PixelFormat);
}
}
具体来说,第四行:
using (Bitmap b = new Bitmap(prefix + tsIndex.ToString() + ".png"))
简化的目标是从精灵集中返回包含精灵的位图,基于精灵集索引和精灵索引。该位图在PictureBox中显示,直到它被更改为不同的图像。我知道逻辑是正确的,问题不在于此。我使用的.png大小为384x256。
所有参数都设置正确,所有引用的文件都在那里,一切似乎都按顺序进行。最奇怪的是有时它起作用,有时它不起作用。这让我相信它可能是System.Drawing本身内存泄漏,但我似乎无法追踪它。
编辑:更新了代码并添加了StackTrace。尽管在不再使用Bitmap时处理它们(请参阅下面的代码示例以了解如何处置Bitmap),但仍然存在相同的问题。
if (Sprite.Image != null) { Sprite.Image.Dispose(); }
Sprite.Image = GetSprite(true, tsIdx, tileIdx);
堆栈跟踪:
System.ArgumentException was unhandled
HResult=-2147024809
Message=Parameter is not valid.
Source=System.Drawing
StackTrace:
at System.Drawing.Bitmap..ctor(String filename)
at CreationTool.Main.GetSprite(Boolean anim, Int32 tsIndex, Int32 tileIdx) in F:\~\~\CreationTool\Main.cs:line 420
at CreationTool.Main.Input_EnemySprite_SelectedIndexChanged(Object sender, EventArgs e) in F:\~\~\CreationTool\Main.cs:line 107
at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)
at CreationTool.States.State_Enemy.populateForm() in F:\~\~\CreationTool\States\State_Enemy.cs:line 28
at CreationTool.States.State_Enemy.Load(String name) in F:\~\~\CreationTool\States\State_Enemy.cs:line 22
at CreationTool.Main.btnLoad_Click(Object sender, EventArgs e) in F:\~\~\CreationTool\Main.cs:line 174
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.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 CreationTool.Program.Main() in F:\~\~\CreationTool\Program.cs:line 15
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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Dispose
方法。位图使用非托管资源,这可能会导致内存压力问题。我建议用 using 语句来封装它。 - pstrjdsMain
方法中添加一个try/catch块,然后将异常信息输出到控制台或记录下来,这样你就可以查看堆栈跟踪并准确地知道它的来源。 - pstrjds