ITKeyword,专注技术干货聚合推荐

注册 | 登录

loops - ArgumentException when trying to draw in c# Form

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

First, i'm not too good known to programming (17 and still learning it), so sorry if my code isn't that good ^^

I've seen something about 2D Engine in C#, and that they're actually not that hard to code, so i wanted to give it a shot, but now i've got a problem:

I created a Panel in the Form, and then it's getting painted I "extract" the Graphics Object, and give it to my "Engine"

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Console.WriteLine("Draw");
        graphics.setG(e.Graphics);
    }

The Engine just "saves" the Graphics object

    public void setG(Graphics g)
    {
        this.g = g;
    }

Now i've got the usual Game Loop, executing an Update and a Draw method every 1/60 Seconds, part of the draw Method:

        for(int i = 0;i < images.Count; i++)
        {
            var curr = images[i];
            g.DrawImage(curr.img, (int)Math.Round(curr.x), (int)Math.Round(curr.y));
        }

Where of course img is an Image, x and y are floats, casted to ints, now i get "unhandled exception of type 'System.ArgumentException' occured in System.Drawing.dll" in the Line

            g.DrawImage(curr.img, (int)Math.Round(curr.x), (int)Math.Round(curr.y));

Exception Detail (Tried to translate a few words, original log is german):

System.ArgumentException was unhandled
  HResult=-2147024809
  Message=Invalid Parameter.
  Source=System.Drawing
  StackTrace:
       at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
       at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y)
       at _2D_Engine.GraphicsEngine.Draw(Graphics g) in C:\Users\Paul\Documents\Visual Studio 2015\Projects\2D Engine\2D Engine\GraphicsEngine.cs:Line 38.
       at _2D_Engine.Form1.gameLoop() in C:\Users\Paul\Documents\Visual Studio 2015\Projects\2D Engine\2D Engine\Form1.cs:Line 54.
       at _2D_Engine.Form1.gameLoop() in C:\Users\Paul\Documents\Visual Studio 2015\Projects\2D Engine\2D Engine\Form1.cs:Line 58.
       at _2D_Engine.Form1.gameLoop() in C:\Users\Paul\Documents\Visual Studio 2015\Projects\2D Engine\2D Engine\Form1.cs:Line 58.
       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()

This happens whatever i'm trying to draw to the Panel, also when I try to draw a Polygon for example, i get the same Exception. Not sure if I misunderstood anything about how to write the Game Loop, because it doesn't happen when i just call the draw Method from the Paint method, but the Paint Method isn't called everytime I want an object to move, so I can't use only it for the Draw Loop, i'm pretty puzzled right now ^^

c# loops drawing draw
|
  this question
asked Mar 4 '16 at 17:24 Potheker 16 1      Do not store and use PaintEventArgs.Graphics outside of the Paint event. Actually do all the painting only in that event. Use Invalidate method when you want to repaint (when you want object to move etc.) –  Ivan Stoev Mar 4 '16 at 17:42      @IvanStoev ok, i've seen a tutorial where the draw-method was called in the game-loop, but whatever... it works when i just call this.Invalidate(); in my game-loop, well kind of... it just repaints when i "use" the window, when i take it and move it on the screen for example, when it stands still, nothing happens, what did i do wrong? ^^ –  Potheker Mar 4 '16 at 18:10

 | 

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。