如何从pictureBox中只删除背景图像并保留我绘制的像素



这是播放按钮代码:

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();

但在这种情况下,轨迹条和计时器移动一次到图像编号2,然后在那里停止,什么也不做。

只有当checkBox被选中时才会发生这种情况,如果它没有被选中,并且在pictureBox中有像素和背景图像,它可以正常工作。

**我修改了cleared并缩短了问题**

您不应该从非持久化方法(如检查更改事件)绘制图形对象。

您应该处理PictureBox的"绘制"事件,以便每次PictureBox需要刷新时(例如更改背景图像时)都重新绘制图形。就目前情况来看,如果你也足够移动窗口,你很可能会丢失你的图形。

例如:

pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
   Graphics g = e.Graphics;
   // draw your pixels here
}

注意:图片框有一些用途,也有一些问题,如果你想进行自定义绘图,我建议你只在标准面板上绘图。

最新更新