如何创建多个图片框并保留它们?

时间:2013-09-15 09:22:49

标签: c# picturebox

我希望每次用户点击主图片框时,我的应用中的用户都可以创建一个能够创建图片框的功能(我想保留图片框并为用户提供无限图片框创建功能)

代码:

PictureBox Pic = new PictureBox();
Pic = pictureBox2;
Pic.Left = e.X;
Pic.Top = e.Y;
Pic.Visible = true;

1 个答案:

答案 0 :(得分:3)

您需要注册点击pictureBox的事件,并在点击时创建新的pirtureBox

     this.pictureBox1 = new System.Windows.Forms.PictureBox();
     this.pictureBox1.Location = new System.Drawing.Point(319, 32);
     this.pictureBox1.Name = "pictureBox1";
     this.pictureBox1.Size = new System.Drawing.Size(100, 50);
     this.pictureBox1.TabIndex = 7;
     this.pictureBox1.TabStop = false;
     // THE IMAGE IS UP TO YOU TO ADD.USE THIS -this.pictureBox1.Image = 
     this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
     this.Controls.Add(pb);

并在pictureBox1_MouseClick事件中执行:

  private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
  {
     PictureBox pb = new System.Windows.Forms.PictureBox();
     pb.Location = new System.Drawing.Point(319, 32);// THE LOCATION AND CONTEXT IS UP TO YOU
     pb.Name = "pictureBox1";
     pb.Size = new System.Drawing.Size(100, 50);
     pb.TabIndex = 7;
     pb.TabStop = false;
     this.Controls.Add(this.pictureBox1);
  }

好吧,我在这里添加rene在评论中说,如果你使用大图片或大量图片,你可能会很快耗尽内存。在这种情况下,您应该.Dispose()对不再需要的图片