It seems you are loading the same bitmap 100 times . There is a memory problem, not 100 PictureBox s. A PictureBox should have low overhead because they do not include the image in memory consumption, it is a reference Bitmap , which is more likely to consume large amounts of memory.
Easily fixed - consider downloading a bitmap once, and then apply it to all your PictureBox s.
Edit:
private void skyInTheWindow() { for (int i = 0; i < 100; i++) {
... in:
private void skyInTheWindow() { var bitmap = new Bitmap("C:/MyPath/Sky.jpg");
You can only have a single PictureBox stretched to the width of the background, but replacing it over time. Of course, you need to draw something on the edge where a space appears.
You may get a little flicker with the repetition of the PictureBox , although this is one of the things I worry about, but it can still serve.
Or what I would do is create a UserControl and override OnPaint and just turn it into a bitmap problem and not have a PictureBox at all. Much faster and more efficient and does not flicker. :) This is purely optional
You also have the opportunity to eliminate any flicker if you first draw on the screen Graphics and Bitmap and "bitlit" the results on a visible screen.
Could you give me some code that serves as a starting point, because it is difficult for me to embed it in the code? I am not very good at graphic programming, and I really want to learn from each other. Better code does not flicker
As requested, I have included the following code:
Flicker Free Offscreen Rendering UserControl
In essence, this is what you need to create an off-screen raster image, which we will draw first. This is the same size as UserControl. The OnPaint control calls DrawOffscreen , passing in Graphics , which is connected to an off-screen bitmap. Here we turn around only by displaying tiles / sky that are visible and ignore others in order to increase productivity.
As soon as all this is done, we will close the entire screen bitmap on the display in one operation. This eliminates:
- Flicker
- Tear effects (usually associated with lateral movement)
There is a Timer that should update the position of all fragments based on the time since the last update. This allows a more realistic movement and avoids acceleration and deceleration under load. Tiles are moved using the OnUpdate method.
Some important properties:
DesiredFps - desired frames / second. This directly controls how often the OnUpdate method is OnUpdate . It does not directly control how often OnPaint is called.
NumberOfTiles - I set it to 100 (cloud images)
Speed - speed in pixels per second; bitmaps move. Associated with DesiredFps . It is load independent; computer independent value
Picture If you noted in the code for Timer1OnTick , I call Invalidate(Bounds); after animating everything. This does not cause immediate coloring, and Windows will pause the drawing operation, which will be performed later. Successive pending operations will be merged into one. This means that we can animate positions more often than draw under heavy load. The animation mechanism is independent of paint . This is a good thing, you do not want to wait for the appearance of colors.
You will notice that I redefine OnPaintBackground and essentially do nothing. I do this because I do not want .NET to erase the background and cause unnecessary flickering before calling my OnPaint . I don’t even want to erase the background in DrawOffscreen , because we are still going to draw bitmap images. However, if the control has been changed more than the height of the raster image of the sky, and if this is a requirement, then you may want to. The performance degradation is pretty slight, I suppose, when you possibly draw a few raster maps anyway.
When you create the code, you can execute it on any Form . The control will be visible on the toolbar. Below I ran it on MainForm .

The control also demonstrates design-time properties and default values, which you can see below. These are settings that seem to work well for me. Try changing them for different effects.

If you installed the control and your form changes, you can resize the application at runtime. Useful for measuring performance. WinForms is not particularly hardware accelerated (unlike WPF), so I would not recommend that the window be too large .
code:
#region using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Windows.Forms; using SkyAnimation.Properties;