-2

I've been searching regarding to this problem but i can't make it work :( here's my code :

using System.Drawing;

namespace Chess.Source
{
    public sealed class GraphicsBuffer
    {
        private Graphics graphics;
        private int height;
        private Bitmap memoryBitmap;
        private int width;

        public Graphics Graphics
        {
            get { return graphics; }
        }

        public void CreateGraphicsBuffer(Graphics g, int W, int H)
        {
            if (memoryBitmap != null)
            {
                memoryBitmap.Dispose();
                memoryBitmap = null;
            }

            if (graphics != null)
            {
                graphics.Dispose();
                graphics = null;
            }

            if (W == 0 || H == 0)
                return;


            if ((W != width) || (H != height))
            {
                width = W;
                height = H;

                memoryBitmap = new Bitmap(width, height);
                graphics = Graphics.FromImage(memoryBitmap);
            }
        }

        public void Render(Graphics g)
        {
            if (memoryBitmap != null)
            {
                g.DrawImage(memoryBitmap, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
            }
        }
    }
}
Soviut
  • 88,194
  • 49
  • 192
  • 260

1 Answers1

0

The flickering is most likely due to the fact that you're not using double buffering.

You can enable double buffering on the forms element you're drawing to by setting it's DoubleBuffering property to true.

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered(v=vs.110).aspx

Soviut
  • 88,194
  • 49
  • 192
  • 260
  • where can i put that code sir? – Akatzuke Nemesis Sep 27 '16 at 21:31
  • Wherever you set properties on controls in your code, most likely the constructor. – Soviut Sep 27 '16 at 22:39
  • i have done the doublebuffering property to true but the thing is my image is not missing and will only appear when i press on the board – Akatzuke Nemesis Sep 28 '16 at 01:27
  • When are you calling the `Render()` method? It seems like you're relying on your window to deal with repaints when you should be actively calling the render method instead. You need to read more deeply into the .NET graphics documentation. – Soviut Sep 28 '16 at 03:18