简体   繁体   中英

Slow drawing of images in pictureBoxes

The form size is stretched to the screen resolution. The following elements are on the form:

private System.Windows.Forms.PictureBox pictureBox6;
private System.Windows.Forms.PictureBox pictureBox7;
private System.Windows.Forms.PictureBox pictureBox8;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox9;
private System.Windows.Forms.PictureBox pictureBox3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Panel panel1;

When you open a form, it immediately loads the images to the panel from the folder. Example:

pictureBox2.Load("bgr/1.png");

Images are loaded and displayed, but they are drawn for a long time, that is, you can see the rendering of the image itself in parts.

Image 1

Image 2

Question: how can I solve the problem of slow drawing images or make it invisible to the user?

There is also a property on the form to enable double buffering that can sometimes fix display issues (particularly if you notice flickering while drawing or going in and out of a form. This doesn't always extend down to the controls on the form (like the Panel). If you want to use the panel, you can still make double buffering work but you'll need to extend the panel and then use the extended panel (I'll share the code, it's straightforward). I suspect if I had to guess that if by moving the pictures out of the panel yours started working faster that this will also work.

Form:

this.DoubleBuffered = true;

Extended Panel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyApp
{
    public class PanelEx : System.Windows.Forms.Panel
    {
        public PanelEx()
        {
            this.SetStyle(
                System.Windows.Forms.ControlStyles.UserPaint | 
                System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | 
                System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, 
                true);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM