C # PictureBox flicker / performance control

I currently work in Windows Form Apps and am doing what is essentially a map editor for the game. The way I did this is the central TabControl , where each TabPage contains a custom PictureBox control, and all other interface elements are located around this central TabControl . PictureBox uses its Paint event to draw everything that is placed on the map, and therefore draws several images of different sizes, revolutions and weights, etc. For a single PictureBox . So far, everything went well. TabPage is essentially used as a viewport for a PictureBox and has a size (1280x720).

The problem is the scale at which the cards are produced. The average (as well as maximum) size of the map on the screen is about 19200x10800 pixels and can consist of hundreds of objects at any point. When drawing only the 19200x10800px background, the PictureBox starts to flicker when it is redrawn and renders the program unusable. Since the map is so large that you can swing around them, and this is where it really flickers. In addition, I do not want to use a 19200x10800px image source, if possible, for the sake of file sizes, and scaled image quality is not a problem at all.

I did a bunch of reading about why this might be, and I feel like I've tried everything up to this point. So far ive tried:

  • Having a background image of only 1920x1080 and scaling it to 10x
  • Starting from the 1920x1080 image, resizing it programmatically and drawing this image
  • Cutting the background into several segments (I tried a lot of different amounts) and drew only those that are visible in the viewer (I tried this for both small (1080p) and large (10800p) images)
  • Using cropped graphics so that only objects on the screen are displayed.
  • Double buffering is used both in the picture and in the form in which the camera is located.
  • Convert the image upon initialization to an “optimized bitmap” with faster formatting, and then draw the bitmap

I probably tried a couple of other things along with a few optimizations, but it made me forget about everything else for so long. From what I read, most likely, something related to redrawing the controls is too slow for some reason of performance or limitation with Picturebox, however, I can’t say exactly what happens due to the lack of experience in form controls .

Currently, for painting the background in the Paint event, I have either:

 g.DrawImage(image, new Rectangle(0, 0, (int)(image.Size.Width * levelScale), (int)(image.Size.Height * levelScale))); 

or

 g.ScaleTransform(levelScale, levelScale); g.DrawImage(image, new Rectangle(0, 0, (int)(image.Size.Width), (int)(image.Size.Height))); 

Where g is the Graphics object.

Did I manage to limit the application capabilities for Win forms, or is there something that I might lose?

Any help is appreciated

Thanks in advance.

+4
source share
3 answers

Just for formality, although I answer my question just in case if someone else would like to know the result.

Based on the overwhelming consensus that winforms just didn't do what I was trying to do with this, I decided that I needed to move to another platform. I was offered WPF, DirectX and OpenGL, but after some extensive searching, I found what I consider to be the best solution.

To harness the power of DirectX hardware acceleration, MS made it possible for you to embed XNA graphics devices in a winforms application. Essentially, you can create custom controls that run in the usual winform style, which has access to a much higher level of graphics control. So (with a bit of extra work), I replaced the picture box that I used with a custom graphic control that processes the entire drawing. This worked very well, and on the other hand, I didn't have to hit too much on my development time.

For those who are looking for more information, refer to this question, which contains additional links that should help. Thanks again to everyone who gave their advice!

+3
source

This is a quoted answer from the URL below. There are code examples on the link below. Hope this is helpful; I'm not sure if you have tried this yet, and perhaps this will help you get a little more juice from your existing Photoshop management. As explained in other answers, it seems that in the near future you will be forced to use a more powerful solution regardless of whether (DirectX / OpenGL or WPF)

** Partial quote from http://social.msdn.microsoft.com/Forums/en-US/68ecd1f6-2eeb-45ce-a881-24c62145ab0e/c-picturebox-problems

“I suppose the real problem is that it takes too much time to redraw images. GDI + is rather slow, it doesn’t use any video hardware acceleration. To speed it up, be sure to avoid drawing scaling and use Format32PArgb format, approximately 10 times faster than any other format. First upload the images to a bitmap with the correct format. "

+1
source

If you have many elements (possibly implemented as controls), forget about the standard Windows Forms event mechanism. Some time ago I wrote an editor / simulator of logical gates, which supported many thousands of gates in the editor and was very fast. There I used the canvas and draw the gates as custom “images” instead of putting them as controls. You will need to write a custom GetUnderlyingGate function that solves the current gate / fragment (is your editor a text editor?) From an array of coordinates. In addition, there were some visible area optimizations.

Perhaps when I get home I will post the source code and notify you.

0
source

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


All Articles