这是播放按钮代码:
private void btnPlay_Click(object sender, EventArgs e)
{
_files = new List<FileInfo>();
_indx = 0;
DirectoryInfo dir = new DirectoryInfo(filesForanimation);
if (_files == null)
_files = new List<FileInfo>();
fi1 = dir.GetFiles("*.bmp");
_files.AddRange(fi1);
_files = _files.OrderBy(f => f.LastWriteTime).ToList();
button14.ForeColor = Color.Red;
button13.ForeColor = Color.Black;
button12.ForeColor = Color.Black;
timer3.Start();
button13.Enabled = true;
button13.Text = "Pause";
button12.Enabled = true;
trackBar1.Maximum = fi1.Length;
trackBar1.Minimum = 0;
}
然后是计时器滴答事件:
private void timer3_Tick(object sender, EventArgs e)
{
try
{
Image iOLd = this.pictureBox1.Image;
trackBar1.Value = _indx;
label23.Text = _files[_indx].Name;
if (checkBox1.Checked)
{
Image img = Image.FromFile(_files[_indx].FullName);
this.pictureBox1.Image = img;
this.pictureBox1.Image = null;
pictureBox1.Refresh();
}
else
{
Image img = Image.FromFile(_files[_indx].FullName);
this.pictureBox1.Image = img;
}
if (iOLd != null)
// iOLd.Dispose();
_indx++;
if (_indx >= _files.Count)
{
_indx = 0;
trackBar1.Value = 0;
}
timer3.Interval = Convert.ToInt32(numericUpDown1.Value);
}
catch
{
}
}
和 checkBox 检查事件:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
Graphics g = Graphics.FromImage(pictureBox1.Image);
g.Clear(SystemColors.Control);
pictureBox1.Invalidate();
}
else
{
pictureBox1.Load(fi[trackBar1.Value].FullName);
pictureBox1.Invalidate();
}
}
问题出在计时器滴答声中。我第一次尝试在 if (checkBox1.Checked) 里面我只做了 pictureBox1.Refresh(); 但是当点击播放按钮时,它会显示带有白色背景的大红色 X。所以我标记了 // 行:iOLd.Dispose(); 所以现在我只看到这些像素的图纸,但它们永远不会改变我猜出于某种原因它不会从硬盘加载新图像,因为它应该这样做它会不断地在同一个图像上移动。
所以我尝试在 if (checkBox1.Checked) 中做现在这样:
Image img = Image.FromFile(_files[_indx].FullName);
this.pictureBox1.Image = img;
this.pictureBox1.Image = null;
pictureBox1.Refresh();
但在这种情况下,trackBar 和计时器移动一次到图像编号 2 并停在那里而不做任何事情。
并且仅当复选框被选中时才会发生这种情况,如果它没有被选中并且在图片框中有像素并且背景图像工作正常。
**我修改了clear并缩短了问题**