简体   繁体   中英

C# bitmap - Parameter is not valid

I am making a basic windowed game in c# and I am unable to create a basic bitmap for the screen. I have a panel over the screen and I want to be able to draw to it, so I am using the following code:

 public partial class GameWIndow : Form
{
    public GameWIndow()
    {
        InitializeComponent();
    }





    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Bitmap buffer;
        buffer = new Bitmap(Width, Height);
        Task.Factory.StartNew(() =>
        {
            using (Graphics g = Graphics.FromImage(buffer))
            {
                g.FillRectangle(new SolidBrush(Color.PaleGoldenrod), 10, 10, 100, 100);
            }
            this.Invoke(new Action(() =>
            {
                this.BackgroundImage = buffer;
            }));
        });
    }

when this runs it gives me the error "Parameter is not valid" for the line

buffer = new Bitmap(Width, Height);

Width and Height are 900 and 700 respectivly, and both integers.

If the problem is that I am doing this entirely wrong, what would be a better way to acomplish this.

--------Edit-------

I have looked at your comments and it seems that what I have done above is very terrible so I tried a different approach, and I am not sure if that is much better.

    private Game game;
    private Thread renderThread;
    private Stopwatch stopwatch = new Stopwatch();

    public GameWindow()
    {
        InitializeComponent();

        //Starts game
        game = new Game();

        //Starts rendering
        this.DoubleBuffered = true;
        renderThread = new Thread(new ThreadStart(draw));
        renderThread.Start();
    }

    //Runs when screen is asked to refresh
    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        //Sends graphics to game class where it draws game
        game.draw(e.Graphics);

    }

    private void draw()
    {
        stopwatch.Start();

        while (true)
        {

            //If 1/60th of a sec has passed
            if (stopwatch.ElapsedMilliseconds > 1000D / 60D)
            {

                stopwatch.Restart();
                Invalidate();

            }
        }

    }

The code above doesn't run correctly, I am not quite sure why, and if someone could help with that, I would appreciate that. However my major question isn't how to fix it, but if this is even a effective or "correct" way to draw.

As @TaW suggests, there are many problems. I assume this is the Paint event handler of a WinForms application? That is important information you should have told us.

The Paint event handler is called by the OS every time anything in your form content needs redrawing. PaintEventArgs supplies you with the ClipRectangle you must draw into and the Graphics context you must draw with. You should only draw to that in your handler; you must not perform any other processing, start threads, or perform any I/O. If you need to load images or perform I/O you must do that outside your handler, save the result to a form property, invalidate the rectangle you want to draw to, then use the saved property to draw in your Paint handler. It should look something like this:

private void Panel1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(_buffer, _position);
}

What's _buffer ? An Image you fill outside the handler. _position ? A location you update outside your handler.

Do not allocate buffers, Brush es, or images inside your Paint handler; create them outside the handler and reuse them. If you need to update your control with the result of a long process or network connection, create a Task or BackgroundWorker outside your handler and have the completion routine save the result and invalidate your control. Use the saved result in your handler. If you need to perform some kind of animation, precreate all the animation frames, use a timer to invalidate your display, and draw the correct frame in your handler.

WinForm graphics is a huge topic, you need to get a book or follow a tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM