I have a WinForm
application 'Bouncing Balls' , and I need to paint the balls
on a bitmap
and present the bitmap on this form.
I have a plusButton
that adds new ball, and i'm saving each new ball in a list.
Now, the Form_Paint
method is telling to each ball to draw himself, it works fine
until there are a lot of balls and the all application become very slow..
Here is my Code:
The paint method of the form code:
private void Form1_Paint(object sender, PaintEventArgs e)
{
ballsArray.drawImage(bmp,e, ClientRectangle);
}
NOTE: ballsArray
is from type AllBalls
, this is a class that wraps the ball methods, inside his c'tor i'm creating a list that keeps each ball. the bmp
, is created when the form is loading - on Form_Load()
method.
The drawImage of ballsArray
code:
public void drawImage(Bitmap bmp,PaintEventArgs e, Rectangle r)
{
foreach (Ball b in allBalls)
{
b.drawImage(bmp,e, r);
}
}
The drawImage
of Ball code:
public void drawImage(Bitmap bmp, PaintEventArgs e, Rectangle r)
{
using (Graphics g = Graphics.FromImage(bmp))
{
e.Graphics.FillEllipse(brush, ballLocation);
g.DrawImage(bmp, 0, 0);
}
}
NOTE: ballLocation
is a rectangle that represent the location of the ball in each
step of movement..
So what I'm doing wrong? What causing the application to be slowly?
I have a constraint to draw everything on the bitmap and present it on the form. I'm also passing the bitmap that I create when the form is loading, because I need to draw each on it.