[英]Update multiple pictureboxes at the same time
我有四个PictureBoxes
(每个PictureBox代表一个骰子)和一个更改每100ms源图片的Timer (在内存中加载为List<Bitmap> imagesLoadedFromIncludedResources
)。
码:
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();
}
}
我需要一次 更改所有图像 - 此时,您可以看到图像从左向右变化,延迟很小 。
我为每个PictureBox
尝试了一个Thread
变种(使用此答案中的 Control.Invoke
方法) - 它在视觉上好一点但不完美。
您可以尝试暂停表单的布局逻辑:
SuspendLayout();
// set images to pictureboxes
ResumeLayout(false);
Parallel.ForEach
(
dices,
new ParallelOptions { MaxDegreeOfParallelism = 4 },
(dice) =>
{
dice.Image = ...;
dice.WaitOnLoad = false;
dice.Refresh();
}
);
问题是UI控件只能从UI线程访问。 如果要使用此方法,则必须创建PictureBox的副本,然后在操作完成后替换UI。
另一种方法是创建两个PictureBox,第一个只在另一个的顶部(隐藏后者)...你改变所有的图像然后,一旦处理完成,你迭代后面的所有图像将它们放在顶部会导致较小的延迟。
我的处理方式不同 - 自从我使用WinForms之后已经有一段时间了,但我可能会更多地控制图像的渲染。
在这个例子中,我将图像全部放在一个垂直堆叠的源位图中,作为资源存储在汇编中:
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);
}
}
}
}
}
如果它有帮助,源代码就在这里: http : //sdrv.ms/Wx2Ets
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.