如何创建多个图片框并保留它们? [英] How to create multiple picture boxes and keep them?

查看:69
本文介绍了如何创建多个图片框并保留它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create an ability for the user in my app to create pictureboxs everytime the user clicks on the main picturebox (I want to keep the pictureboxs and give an infinity picturebox creating ability to the User)

代码:

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

推荐答案

您需要注册单击pictureBox的事件,并在单击它时创建一个新的pirtureBox:

you need to register to the event of clicking the pictureBox and create a new pirtureBox when clicking on it:

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

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

well, I'm adding here as rene saying in the comment, that if you are using big pictures or large amount of pictures you might run out of memory very fast. in that case you should do .Dispose() to pictures you no longer need

这篇关于如何创建多个图片框并保留它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆