Image drawing speed

I am working on a game, but I am currently using tests.

If anyone can help me with this, I would really appreciate it.

What I do, I fire the paint event on the panel when I press the start button using this code:

private void startToolStripMenuItem_Click(object sender, EventArgs e) { try { pnlArea.Invalidate(); } catch (Exception) { throw; } } 

Then I do this in my drawing event:

  private void pnlArea_Paint(object sender, PaintEventArgs e) { try { stopwatch = new Stopwatch(); // Begin timing stopwatch.Start(); if (gameStatus == GameStatus.PlaceHead) { e.Graphics.DrawImage(dictHead["HeadRight"], 100, 100, 15, 15); } //e.Graphics.Clear(Color.White); if (gameStatus == GameStatus.GameTest) { int x = 0; int y = 0; for (int i = 0; i < 5000; i++) { x += 15; if (x > 1000) { x = 0; y += 15; } e.Graphics.DrawImage(body.Value, x, y, 15, 15); } } toolTimer.Text = Math.Round((stopwatch.Elapsed.TotalMilliseconds / 1000), 2).ToString() + "s"; // Stop timing stopwatch.Stop(); } catch (Exception) { throw; } } 

This is the part of the body that I draw in the code above:

enter image description here

This is the exact size → 15px x 15px

but sometimes it takes up to 1.2 seconds !!! is there any way to improve this?

This is an example of the final result screen: enter image description here

+5
source share
2 answers

In addition to the information that everyone gave me, I came to the conclusion to double the panel buffer. This fixed my problem →

 class DoubleBufferedPanel : Panel { public DoubleBufferedPanel() : base() { DoubleBuffered = true; } } 

And I just used this double buffer bar.

New test without any flicker !: enter image description here

+1
source

You need to think about how to minimize the number of drawing calls you make. You are currently typing 5,000 small boxes to create a grid. Each time you draw a window, you follow a few instructions and then call the graphical method to render the scaled image. This is a lot of overhead for each square of the grid.

So, the first thing you could pay attention to is to find more effective ways of drawing an image - for example, DrawImageUnscaled can work faster than DrawImage and achieve the desired result. But this is an optimization of an ineffective algorithm - what you need to do to get the real performance benefit is to see if you can adopt a new, more efficient algorithm.

If you need to render using bitmaps, see how the pattern repeats - can you make a larger bitmap that provides a group of 4x4 or 16x16 cells and do it? Or a bitmap representing an entire column or row? Then you can make 50 calls instead of 5000.

But if you don't need to use bitmap rendering, you can do much better. For example, if you gfx.Clear(backgroundColor) and then draw about 140 black lines down and across, you can create the same display with 141 calls. Or, if you draw about 70 rectangles, you can effectively execute 2 lines per call. This greatly reduces the number of method calls you must make and allows the graphics system to draw more pixels in a batch using optimized lines for rendering and rendering rectangles (in fact, a rectangle can work much faster than a generalized line because of the system, knowing that the lines always vertical and horizontal).

(If there are bits that do not match this pattern, can you still display the background grid and then draw the changes on top?)

Further, if only small areas of the image change from one frame to another, then your algorithm will display 5,000 boxes, although 4999 of them do not change (or 70 rectangles when 1 is enough). This way, you can significantly improve the situation if you (a) just cancel the part of the view that needs to be changed, and (b) write your rendering procedure to determine which grid squares are outside the clip, and therefore it makes no sense to draw. This can reduce your updates by drawing 1 rectangle instead of 5000 frames. (Another way to achieve the same thing is to save the image in a bitmap off-screen and simply draw changes on it. When you render it on the main screen, the graphics card will pin it for you and achieve the same result - a much faster redrawing speed)

All about achieving the same display, being "lazy" and thinking alternately of doing as little work as possible. (Getting a computer to go faster always comes down to asking it to do less)

+5
source

Source: https://habr.com/ru/post/1210272/


All Articles