简体   繁体   English

System.ObjectDisposedException与图形对象

[英]System.ObjectDisposedException with graphics object

I have to write a game in C# using a Windows form. 我必须使用Windows窗体以C#编写游戏。 For my drawing method on my screen, I'm getting a weird System.ObjectDisposedException which spawns upon one of my graphics objects being instantiated.Here is the problematic code: 对于屏幕上的绘制方法,我得到一个奇怪的System.ObjectDisposedException,该异常会在实例化我的一个图形对象时生成,这是有问题的代码:

// Graphics variables.
Graphics graphics;
Graphics backBufferObject;
Bitmap backBuffer;
// Graphics and backBuffer are instantiated, elsewhere, in the constructor.

private void DrawScreen()
{
    // Copy the BackBuffer to Graphics Object.
    using (graphics = Graphics.FromImage(backBuffer))
    using (backBufferObject = this.CreateGraphics())
    {
        // Draw BackBuffer to screen (buffer switching).                  
        backBufferObject.DrawImage(backBuffer, 0, 0, this.Width, this.Height);

        graphics.DrawImage(Properties.Resources.scary_pacman, new Rectangle(
            this.Width / 2 , this.Height / 2, 32, 32));

        graphics.Clear(Color.Thistle); // Clear BackBuffer for efficient rendering.           
    }                       
}

The problematic line, which throws a System.ObjectDisposedException is the line: using (backbufferObject = this.CreateGraphics()) . 有问题的行(抛出System.ObjectDisposedException)是该行: using (backbufferObject = this.CreateGraphics()) I'm not sure why this exception is being thrown at this particular point due to the fact that this object is being reinstantiated at this point, whereas it was disposed at the end of the using brackets. 我不确定为什么在这一特定时刻抛出此异常,因为在这一点上实例化了该对象,而将其放置在使用方括号的末尾。 So far I've tried putting those two lines in using statements due to the fact that they are affected by IDisposable. 到目前为止,由于它们受到IDisposable的影响,因此我尝试将这两行放入using语句中。

It'd probably be noteworthy that the error is being thrown after I close the Windows form during Run time. 可能值得注意的是,在运行时关闭Windows窗体 ,将引发错误。 So why is this Graphics object being disposed of in this particular instance? 那么为什么在这个特定实例中处理此Graphics对象呢?

Full code: http://pastebin.com/mSa6XCpP 完整代码: http//pastebin.com/mSa6XCpP

If your form has been disabled, the this referenced in the line of code the exception happens on: 如果您的表单已被禁用,则在发生以下情况的代码行中引用this

using (backbufferObject = this.CreateGraphics())

... will like already be disposed. ...将会被处置。 So it isn't the Graphics object that's being disposed. 因此,不会处理Graphics对象。 It's the form. 这是表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.ObjectDisposedException:无法访问已处置的对象 - System.ObjectDisposedException: Cannot access a disposed object 尝试列出对象时出现System.ObjectDisposedException - System.ObjectDisposedException when trying to list an object System.ObjectDisposedException UWP - System.ObjectDisposedException UWP System.ObjectDisposedException - System.ObjectDisposedException System.ObjectDisposedException异常 - System.ObjectDisposedException System.ObjectDisposedException:无法访问Xamarin中的已处置对象 - System.ObjectDisposedException: Cannot access a disposed object in Xamarin System.ObjectDisposedException 从 xml 反序列化 object - System.ObjectDisposedException while deserializing object from xml System.ObjectDisposedException:无法访问已处置的对象。 AplicationDbContext - System.ObjectDisposedException: Cannot access a disposed object. AplicationDbContext httpclient.postasync - System.ObjectDisposedException:无法访问已处理的对象 - httpclient.postasync - System.ObjectDisposedException: Cannot access a disposed object System.ObjectDisposedException:'无法访问已处置的对象。' - System.ObjectDisposedException: 'Cannot access a disposed object.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM