简体   繁体   English

同时更新多个图片框

[英]Update multiple pictureboxes at the same time

I have four PictureBoxes (each PictureBox represents one dice) and a Timer that changes every 100ms source pictures (loaded in memory as List<Bitmap> imagesLoadedFromIncludedResources ). 我有四个PictureBoxes (每个PictureBox代表一个骰子)和一个更改每100ms源图片的Timer (在内存中加载为List<Bitmap> imagesLoadedFromIncludedResources )。

在此输入图像描述

Code: 码:

private List<PictureBox> dices = new List<PictureBox>();

private void timer_diceImageChanger_Tick(object sender, EventArgs e)
{
    foreach (PictureBox onePictureBox in dices)
    {
        oneDice.WaitOnLoad = false;
        onePictureBox.Image = //... ;
        oneDice.Refresh();
    }
}  

I need to change all the images at once - at this moment, you can see that the images are changing from left to right with a small delay . 我需要一次 更改所有图像 - 此时,您可以看到图像从左向右变化,延迟很小

I tried variant with one Thread for each PictureBox (using Control.Invoke method from this answer ) - it is visually little better but not perfect. 我为每个PictureBox尝试了一个Thread变种(使用此答案中的 Control.Invoke方法) - 它在视觉上好一点但不完美。

You can try to suspend form's layout logic: 您可以尝试暂停表单的布局逻辑:

SuspendLayout();
// set images to pictureboxes
ResumeLayout(false);
Parallel.ForEach
(
    dices,
    new ParallelOptions { MaxDegreeOfParallelism = 4 },
    (dice) =>
    {
        dice.Image = ...;
        dice.WaitOnLoad = false;
        dice.Refresh();
    }
);

The problem is that UI controls can only be accessed from the UI thread. 问题是UI控件只能从UI线程访问。 If you want to use this approach, you must create a copy of your PictureBoxes and then replace the UI ones once the operation is done. 如果要使用此方法,则必须创建PictureBox的副本,然后在操作完成后替换UI。

Another approach would be creating two PictureBoxes, with the first one just on the top of the other one (hiding the latter)... you change all the images and then, once the processing is complete, you iterate all the ones in the back putting them on the top which would result in a lesser delay. 另一种方法是创建两个PictureBox,第一个只在另一个的顶部(隐藏后者)...你改变所有的图像然后,一旦处理完成,你迭代后面的所有图像将它们放在顶部会导致较小的延迟。

I'd approach this differently - it's been a while since I've played with WinForms stuff, but I'd probably take more control over the rendering of the images. 我的处理方式不同 - 自从我使用WinForms之后已经有一段时间了,但我可能会更多地控制图像的渲染。

In this example I've got the images all in one source bitmap stacked vertically, stored as a resource in assembly: 在这个例子中,我将图像全部放在一个垂直堆叠的源位图中,作为资源存储在汇编中:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Timer timer;

        private Bitmap dice;

        private int[] currentValues = new int[6];

        private Random random = new Random();

        public Form1()
        {
            InitializeComponent();
            this.timer = new Timer();
            this.timer.Interval = 500;
            this.timer.Tick += TimerOnTick;

            this.dice = Properties.Resources.Dice;
        }

        private void TimerOnTick(object sender, EventArgs eventArgs)
        {
            for (var i = 0; i < currentValues.Length; i++)
            {
                this.currentValues[i] = this.random.Next(1, 7);
            }

            this.panel1.Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.timer.Enabled)
            {
                this.timer.Stop();
            }
            else
            {
                this.timer.Start();
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(Color.White);

            if (this.timer.Enabled)
            {
                for (var i = 0; i < currentValues.Length; i++)
                {
                    e.Graphics.DrawImage(this.dice, new Rectangle(i * 70, 0, 60, 60), 0, (currentValues[i] - 1) * 60, 60, 60, GraphicsUnit.Pixel);
                }
            }
        }
    }
}

The source is here if it helps: http://sdrv.ms/Wx2Ets 如果它有帮助,源代码就在这里: http//sdrv.ms/Wx2Ets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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