0

I have written a tetris game in C#.I'm drawing the shapes in picturebox by using below code.

graphics g
g = pictureBox1.CreateGraphics();

I'm drawing the next step of the shapes by cleaning every second with through timer code. My question is:it's working slowly picturebox.refresh() command and distorted display.What should I do?

I tried below function.But,it's not working.I have same problem.

public void EnableDoubleBuffering()
    {
        // Set the value of the double-buffering style bits to true.
        this.SetStyle(ControlStyles.DoubleBuffer |
           ControlStyles.UserPaint |
           ControlStyles.AllPaintingInWmPaint,
           true);
        this.UpdateStyles();
    }
MaxCoder88
  • 2,374
  • 6
  • 41
  • 59
  • Always draw with the Paint event, never use CreateGraphics(). Call the pb's Invalidate() method to trigger a repaint. That also automatically enables double buffering. The method you tried enables double-buffering on the form, not the picture box. PB already has double buffering turned on. Why it is slow is unguessable. – Hans Passant Jul 30 '11 at 14:36
  • @Hans Passant- First of all,thanks for your interesting.As I mentioned,the display of the tetris game is not fluent.So,Refresh is not good enough.What require exactly I do? – MaxCoder88 Jul 30 '11 at 14:51
  • Use a profiler to find out what code that you wrote is slow. – Hans Passant Jul 30 '11 at 15:01

2 Answers2

1

The way you are drawing, you are using the GDI to draw the pictures (basically using software to render game graphics). This is going to be slow compared to normal games, because most games use DirectX or OpenGL to blit the data to the video card very fast at the hardware level. I would recommend learning Microsoft XNA and writing your game in XNA, which is based upon C# and offers a nice content pipeline to do exactly what you are wanting to do.

David C
  • 3,610
  • 3
  • 21
  • 23
  • It just occurred to me that if you are using any kind of transparency in the image, that would slow the drawing down significantly as well. Alpha blending is ridiculously slow in GDI. – David C Jul 30 '11 at 15:00
  • As a test to believe what I am saying, create a simple form, set it's background image to some type of gradient, then put a couple of Group Boxes with a few controls a piece on the form. Now, set the GROUPBOX background color to "TRANSPARENT". This will be a test of how the GDI performs alpha blending, you will notice it will slowly paint the groupboxes on the screen. This is because the GDI cannot handle alpha blending with any speed. Alpha Blending is essential for most games. – David C Jul 30 '11 at 15:13
0

I had the same problem. The solution is that you must draw your new picture in another bitmap and when it completes, draw it in your picturebox.

Smi
  • 13,850
  • 9
  • 56
  • 64
sadegh saati
  • 1,168
  • 1
  • 13
  • 24