每次都没有绘制到图片框的行

时间:2013-07-17 22:04:00

标签: c# graphics drawing picturebox pen

当我的代码只运行大约1/3时,它实际上会正确绘制,剩下的就是不会绘制一些线条。

Pen pen = new Pen(Color.Black);
        Graphics graphics = MainFrm.HangmanImage.CreateGraphics();
        switch (NoOfGuesses)
        {
            case 1://Bottom left leg
                graphics.DrawLine(pen, 15, 60, 40, 60);
                break;
            case 2://Bottom right leg
                graphics.DrawLine(pen, 40, 60, 55, 60);
                break;
            case 3://Middle stock
                graphics.DrawLine(pen, 34, 20, 34, 60);
                break;
            case 4: //Top line
                graphics.DrawLine(pen, 34, 20, 55, 20);
                break;
            case 5: //Rope
                graphics.DrawLine(pen, 55, 20, 55, 25);
                break;
            case 6: //Head
                graphics.DrawEllipse(pen, (new Rectangle { X=52, Y=25, Height=5, Width=5 }));
                break;
            case 7: //Body
                graphics.DrawLine(pen, 55, 30, 55, 35);
                break;
        }
        pen.Dispose();
        graphics.Dispose();

2 个答案:

答案 0 :(得分:0)

没有足够的信息可以肯定,但是,我首先要确保您的变量'NoOfGuesses'始终是1-7之间的值,值0或8将最终无效。其次,我会添加一个'默认'案例,以防你的'NoOfGuesses'出于任何原因超出该范围,并且还会画一条线。

答案 1 :(得分:0)

我通过在表单的paint事件上运行此代码来解决这个问题,似乎只要它重新刷新表单就会忘记它。