winform program background image splash screen problem

problem background

In industrial control projects, it is often necessary to load a background image for simulating equipment or drawings, and some labels or buttons need to be dynamically placed on it. The usual practice is to use the Panel component to
load the background image by setting the BackgroundImage property, which is a common problem Yes, when the window is resized or the Label is dynamically added/deleted, the interface will have a very obvious screen flicker phenomenon.

public void loadPicture(string fileName)
{
    pnlContainer.BackgroundImageLayout = ImageLayout.Stretch;
    pnlContainer.BackgroundImage = null; 
    if (string.IsNullOrWhiteSpace(fileName) == false)
    {
        pnlContainer.BackgroundImage = Image.FromFile(fileName); 
    }
}

Solution: use pictureBox to load the background image

Add a new pictureBox component in the container panel, use the pictureBox component to load the background image, the pictureBox component itself has been optimized, and can solve the problem of screen flashing well. The only problem is that the pictureBox component cannot be used as a container, and the z of the dynamically generated label component The -order direction is always covered by the pictureBox, so after dynamically generating the label, pictureBox1.SendToBack() ensures that the pictureBox1 is placed at the bottom layer.

public void loadPicture(string fileName)
{            
    pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
    pictureBox1.BackgroundImage = null;
    if (string.IsNullOrWhiteSpace(fileName) == false)
    {
        pictureBox1.BackgroundImage = Image.FromFile(fileName);
    }
}

//在调用 newManyLabels() 之后, 再调用 pictureBox1.SendToBack(), 确保 pictureBox1 放置到最下层, 形成背景效果.

Solution: use timer to reduce repaint frequency

When the window size changes, the panel is not refreshed immediately. You can use Timer to delay the refresh, such as 50 milliseconds. This coalesces refresh requests caused by multiple resizes, avoiding overdraw and flickering.

Timer timer = new Timer();
timer.Interval = 50;
timer.Tick += (s, e) => panel.Invalidate();

Solution: use double buffering technology

It doesn't work in my scenario: http://csharp.tips/tip/article/852-how-to-prevent-flicker-in-winforms-control https://chuxing.blog.csdn.net/article/details /38313575

Guess you like

Origin blog.csdn.net/csdnharrychinese/article/details/131040494