如何删除自动生成的图片框内的图像?

时间:2013-01-08 23:36:09

标签: c# .net winforms

我想删除选中鼠标点击的图片框内的图片(自动生成),所以我可以用删除键或上下文菜单删除...

这里是代码:

private void button1_Click(object sender, EventArgs e)
    {
 string theimage = AppDomain.CurrentDomain.BaseDirectory + @"allimages";
 string[] images = Directory.GetFiles(theimage, "*.png");
                int aa;
                for (aa = 1; aa < images.Count(); aa++)
                {
                    PictureBox myPicBox = new PictureBox();
                    myPicBox.Location = new Point(7, 240);
                    myPicBox.Width = 100;
                    myPicBox.Height = 77;
                    myPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                    myPicBox.Margin = new Padding(3, 3, 3, 3);
                    myPicBox.Visible = true;
                    myPicBox.Image = new Bitmap(images[aa]);
                    this.flowLayoutPanel1.Controls.Add(myPicBox);
                    //myPicBox.Click += new EventHandler(curPb_Click);
                    //myPicBox.MouseUp += new MouseEventHandler(myPicBox_MouseUp);
                    myPicBox.MouseDown += new MouseEventHandler(myPicBox_MouseDown);
                    myPicBox.MouseLeave += new EventHandler(mmm_Leave);
                }


        }
        //private PictureBox senderAsPictureBox = null;
        private void mmm_Leave(object sender, EventArgs e)
        {
            PictureBox senderAsPictureBox = sender as PictureBox;
            senderAsPictureBox.BackColor = Color.Empty;
        }
        private void myPicBox_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox senderAsPictureBox = sender as PictureBox;
            MessageBox.Show(senderAsPictureBox.ToString());
            senderAsPictureBox.BackColor = Color.AliceBlue;
        }

这就是我想做的事情

enter image description here

LOGIC:

  

用户在图片框内选择图像拇指 - &gt; USER按[删除]键时 - >删除所选图像

2 个答案:

答案 0 :(得分:0)

我不明白你的问题。 如果你想清除它,请使用:

senderAsPictureBox.Image = null;
senderAsPictureBox.Invalidate();

编辑: 设置图像后,将控件的名称设置为imagepath:

                myPicBox.Name = images[aa].ToString();

还要创建一个新的Eventhandler来处理你的KeyDownEvent

     myPicBox.PreviewKeyDown += new reviewKeyDownEventHandler(myPicBox_PreviewKeyDown);

使用这种方法:

void myPicBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    PictureBox senderAsPictureBox = sender as PictureBox;
    File.Delete(senderAsPictureBox.Name);
}

在MouseDownHandlerMethod中,您可以使用以下命令对焦:             senderAsPictureBox.Focus();

答案 1 :(得分:0)

从此处找到解决方案Get Picturebox Path和SubNatural答案

所以,我会在这里留下代码,以了解谁可能需要它

   private void button1_Click(object sender, EventArgs e)
    {
 string theimage = AppDomain.CurrentDomain.BaseDirectory + @"allimages";
 string[] images = Directory.GetFiles(theimage, "*.png");
                int aa;
                  for (aa = 1; aa < images.Count(); aa++)
            {
                PictureBox myPicBox = new PictureBox();
                myPicBox.Location = new Point(7, 240);
                myPicBox.Width = 100;
                myPicBox.Height = 77;
                myPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                myPicBox.Margin = new Padding(3, 3, 3, 3);
                myPicBox.Visible = true;
                FileStream fs = new FileStream(images[aa], FileMode.Open, FileAccess.Read);
                myPicBox.Image = Image.FromStream(fs);
                myPicBox.Name = images[aa];
                fs.Close();
                this.flowLayoutPanel1.Controls.Add(myPicBox);
                //myPicBox.Click += new EventHandler(curPb_Click);
                //myPicBox.MouseUp += new MouseEventHandler(myPicBox_MouseUp);
                myPicBox.MouseDown += new MouseEventHandler(myPicBox_MouseDown);
                myPicBox.MouseLeave += new EventHandler(mmm_Leave);
                myPicBox.PreviewKeyDown += new PreviewKeyDownEventHandler(myPicBox_PreviewKeyDown);

            }


        }
        private void myPicBox_MouseDown(object sender, MouseEventArgs e)
    {
        PictureBox senderAsPictureBox = sender as PictureBox;
        senderAsPictureBox.Focus(); // binding for clicking
        senderAsPictureBox.BackColor = Color.AliceBlue;
    }
    void myPicBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        PictureBox senderAsPictureBox = sender as PictureBox;
        if (e.KeyCode == Keys.Delete)
            senderAsPictureBox.Image = null;
            senderAsPictureBox.Invalidate();
            senderAsPictureBox.Dispose();
            File.Delete(senderAsPictureBox.Name);
    }

感谢大家帮助我... :)特别是@SubNatural