Question Rendering problem


New member
Oct 23, 2015
Programming Experience
I was following someone's youtube instructional videos on how to get the framework for a 2D game setup, and I don't know what I did wrong, but on his video the image does not flash, and in my game the sprite flashes in and out of existence.

Here is the code I am using to implement:

private void render()        {
            Bitmap frame = new Bitmap(1200, 700);
            Graphics frameGFX = Graphics.FromImage(frame);

            while (true)
                Bitmap Spherecraft = Forcewave2.Properties.Resources.Spherecraft;
                frameGFX.DrawImage(Spherecraft, 100, 100);
                drawHandle.FillRectangle(new SolidBrush(Color.Black), 0, 0, 1200, 700);
                drawHandle.DrawImage(frame, 0, 0);

Any idea what I am doing wrong?

Is that really the code they provided? I can see a number of issues there, some worse than others.

Firstly, it's poor form to keep going back to Properties.Resources multiple times because that will create a new object each time. You should extract the resource to an object only once and then use that object repeatedly.

Secondly, you are creating a SolidBrush inside a loop with the same properties each time and you're not disposing it. If you're going to create a disposable object then make sure you dispose it when you're done with it. If you're going to use an object inside a loop with the same properties every time then just create it once, outside the loop, and then use that one instance repeatedly inside the loop. You don't need to create a SolidBrush that way anyway. The Brushes class has a Black property that returns a SolidBrush that's Black and doesn't need you to dispose it.

As for the game loop, it's not something that I've ever done so I'm not sure what's involved. Is that method executed on a secondary thread? What type of control are you drawing on?
Top Bottom