M
M
MinasM2016-06-26 02:44:09
Programming
MinasM, 2016-06-26 02:44:09

How to increase drawing speed in C#?

For the sake of interest and mastering C #, I am trying to implement a chessboard (for the entire size of the form, and the form is full screen) by means of drawing on the form, but the result is not very pleasing - it is noticeable how the chessboard is drawn during the loading process.
How can you speed this up? Is there any faster way to draw

private void DrawRectangle(int x0, int y0, int x1, int y1, string colohtml)
        {
            label1.Visible = false;

            SolidBrush myBrush = new SolidBrush(ColorTranslator.FromHtml(colohtml));
            Graphics formGraphics;
            formGraphics = this.CreateGraphics();
            formGraphics.FillRectangle(myBrush, new Rectangle(x0, y0, x1, y1));
            myBrush.Dispose();
            formGraphics.Dispose();


        }



        private void shahmatSmall()
        {
            int h, w, h1, w1, X, Y, kolg, kolh;
            h = this.Height;
            w = this.Width;
            kolg = w / 20;
            kolh = h / 20;

            for (c = 1; c <= kolg+20; c=c+1)
            {
                for (d = 1; d <= kolh+20; d=d+1)
                    {
                        X = c*20;
                        Y = d * 20;
                             if ((c+d)%2==0) 
                                {
                                    DrawRectangle(X - 20, Y - 20, X, Y, "#000000");
                                    
                                }
                             else 
                                {
                                     DrawRectangle(X - 20, Y - 20, X, Y, "#FFFFFF");
                                    
                                }
                    }          
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rou1997, 2016-06-26
@MinasM

Redo the code, this should all be done only 1 time:

SolidBrush myBrush = new SolidBrush(ColorTranslator.FromHtml(colohtml));
            Graphics formGraphics;
            formGraphics = this.CreateGraphics();
            
            myBrush.Dispose();
            formGraphics.Dispose();

You can also use double buffering, then rendering will not be visible, it will take place in memory, then the finished image will be redrawn to the form, and if the graphics in the program are generally "heavy", then DirectX / OpenGL or WPF (DirectX) makes sense.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question