我怎样才能从pictureBox中删除背景图像并留下我绘制的像素?

时间:2012-07-26 16:58:32

标签: c#

这是播放按钮代码:

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并停在那里而不做任何事情。

只有在检查了checkBox是否未选中时才会发生这种情况,并且在pictureBox中有像素和背景图像可以正常工作。

**我修改了清除并将问题缩短了**

1 个答案:

答案 0 :(得分:1)

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

您应该处理PictureBox的Paint事件,以便每次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
}

注意:图片框有使用和存在问题,如果您想进行自定义绘图,我建议您只需绘制到标准面板上。