OnPaint event is slow and throws "shadows"

123 views Asked by At

I'm currently trying to draw some sprites on my WinForms with the OnPaint Event. I used a PictureBox before and it worked but because I could not handle the overlapping of multiple PictureBoxes I switched to the OnPaint Event. I looked around the Internet but could not find an answer for my problem.

all sprites I have are embedded resources in my projects. I put them in a List of type 'image' and loop over them when pressing a certain button (KeyEvent).

I have a Timer_Tick Event that looks like this:

     private void Timer_Tick(object sender, EventArgs e)
    {
        Invalidate();
    }

My OnPaint method is like this:

    protected override void OnPaint(PaintEventArgs e)
    {
        DoubleBuffered = true;

            if (SPRITE_POS == 0)
            DrawSprite(spriteList[SPRITE_POS]);

            if (SPRITE_POS != spriteList.Count - 1)
            SPRITE_POS++;
    }

And the DrawSprite Method:

    private void DrawSprite(Image img)
    {
        Bitmap bitmap = new Bitmap(img);
        bitmap.SetResolution(60, 60);
        PEA.Graphics.DrawImage(bitmap, P1_X, P1_Y);
    }

For somehow reason I get this results for fast keypresses (Tried this with images from Sub-Zero from MK):

Shadow

Without Double Buffering it is flickering. With Double Buffering I'm getting this result above. It throws 'shadows'. Is it possible to avoid this easily? The last sprite is still getting displayed for a moment when the new sprite is already drawn on the form.

Thank you so much!

0

There are 0 answers