Sign in to follow this  
Chris27

doublebuffered exception

Recommended Posts

I'm getting an exception error when setting doublebuffered to true in C#. I'm not sure why it's happening. It says invalid argument exception.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SimpleGame
{
    public partial class frmSimpleGame : Form
    {
        private Bitmap m_objDrawingSurface;
        private Graphics objGraphics;
        private SolidBrush solidBrush = new SolidBrush(Color.Black);
        
        public frmSimpleGame()
        {
            //this.DoubleBuffered = true;
            InitializeComponent();
        }

        private void frmSimpleGame_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(m_objDrawingSurface, 0, 0,
            m_objDrawingSurface.Width,
            m_objDrawingSurface.Height);
            e.Graphics.Dispose();
        }

        private void frmSimpleGame_Load(object sender, EventArgs e)
        {
            
            //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            m_objDrawingSurface = new Bitmap(this.ClientSize.Width, this.ClientSize.Height,
                System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            objGraphics = Graphics.FromImage(m_objDrawingSurface);
            objGraphics.Clear(Color.Aqua);
        }

        private void frmSimpleGame_MouseMove(object sender, MouseEventArgs e)
        {

            
            objGraphics = Graphics.FromImage(m_objDrawingSurface);
            objGraphics.Clear(Color.Aqua);
            objGraphics.FillEllipse(solidBrush, e.X - 25, e.Y - 25, 50, 50);
            this.Invalidate();
            lblX.Text = string.Concat("X:" + (e.X - 25));
            lblY.Text = string.Concat("Y:" + (e.Y - 25));
        }

        private void frmSimpleGame_FormClosed(object sender, FormClosedEventArgs e)
        {
            solidBrush.Dispose();
            objGraphics.Dispose();
        }
    }
}

The project works fine if I take out doublebuffered = true. I've tried it in the load event. I've also tried using the setstyle method and it gives the same exception error. Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this