さらにテストを行った結果、この問題は、ビットマップに複製して表示するのに間に合うように画像がロードされていないことが原因である可能性があることがわかりました。これは可能ですか、それとも不可能ですか?
注: はい、タイトルにこのエラーに関する他の質問がありますが、少し調査したところ、多くの原因が考えられるあいまいなエラーのようです。私と同じシナリオの質問は見つかりませんでした。
次のエラーが表示されます。
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);
}
}
具体的には、4 行目:
using (Bitmap b = new Bitmap(prefix + tsIndex.ToString() + ".png"))
コードの単純化された目的は、スプライトセット インデックスとスプライト インデックスに基づいて、スプライトセットからスプライトを含むビットマップを返すことです。このビットマップは、別のイメージに変更されるまで PictureBox に表示されます。ロジックが機能することは事実として知っています。それはここでは問題ではありません。テストに使用している .png は 384*256 です。
すべてのパラメータが適切に設定され、すべての参照ファイルがそこにあり、すべてが順調に進んでいるようです。最も奇妙なことは、機能する場合と機能しない場合があることです。これにより、それ自体がメモリリークである可能性があると信じるようになりましたがSystem.Drawing
、追跡できないようです。
編集: コードを更新し、StackTrace を追加しました。ビットマップが使用されなくなったときにビットマップを破棄しても、同じ問題が発生します (ビットマップの破棄方法の例については、以下のコードを参照してください)。
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()