none
How do I transfer an image from one form to another

    Question

  • I am fairly new to windows forms.  I am using VS2010 and C#.  Could someone explain to me how I can transfer a photo taken with a webcam (not pulling image from a file) on one form (PictureForm.imgCapture.picturebox) to another form (NewVisitorForm.Photo.picturebox).  I have a "save image" button on the Picture form that I would like to click and transfer the image from the Picture form to the NewVisitor form.  Thank you in advance for you help.

    Ant

    Tuesday, August 21, 2012 9:44 PM

Answers

  • ... so your forms (with default names) could look like:

    Form1

        public partial class Form1 : Form
        {
            private System.Windows.Forms.PictureBox pictureBox1 = new System.Windows.Forms.PictureBox();
            private System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
    
            Form2 _form2 = null;
            public Form1()
            {
                InitializeComponent();
    
                this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
    
                //Init usually done by the designer
                // 
                // pictureBox1
                // 
                this.pictureBox1.Location = new System.Drawing.Point(25, 13);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new System.Drawing.Size(171, 133);
                this.pictureBox1.TabIndex = 0;
                this.pictureBox1.TabStop = false;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(154, 203);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                this.ClientSize = new System.Drawing.Size(284, 262);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.pictureBox1);
                this.Load +=new EventHandler(Form1_Load);
            }
    
            void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (this.pictureBox1.Image != null)
                    this.pictureBox1.Image.Dispose();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //add a valid image here...
                this.pictureBox1.Image = Properties.Resources._71f_tgi;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (_form2 == null || _form2.IsDisposed)
                    _form2 = new Form2();
    
                _form2.Show();
    
                _form2.AddPicture(this.pictureBox1.Image);
            }
        }

    Form2:

        public partial class Form2 : Form
        {
            private System.Windows.Forms.PictureBox pictureBox1 = new System.Windows.Forms.PictureBox();
    
            public Form2()
            {
                InitializeComponent();
                this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
    
                // 
                // pictureBox1
                // 
                this.pictureBox1.Location = new System.Drawing.Point(12, 12);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new System.Drawing.Size(260, 166);
                this.pictureBox1.TabIndex = 0;
                this.pictureBox1.TabStop = false;
                this.ClientSize = new System.Drawing.Size(284, 262);
                this.Controls.Add(this.pictureBox1);
            }
    
            void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (this.pictureBox1.Image != null)
                    this.pictureBox1.Image.Dispose();
            }
    
            internal void AddPicture(Image img)
            {
                Image iOld = this.pictureBox1.Image;
                this.pictureBox1.Image = (Image)img.Clone();
    
                if (iOld != null)
                    iOld.Dispose();
            }
        }

    Regards,

      Thorsten

    • Marked as answer by Angry_Ant Wednesday, August 22, 2012 1:25 PM
    Tuesday, August 21, 2012 10:40 PM

All replies

  • Hi,

    there are several ways of achieving this. You could for instance from the buttons click handler call a public Method of the secondFrom that takes that bitmap as parameter and do the needed actions like assigning it (or a copy of it, if the original of the first form gets disposed) to the picturebox.

    Regards,

      Thorsten

    Tuesday, August 21, 2012 10:29 PM
  • ... so your forms (with default names) could look like:

    Form1

        public partial class Form1 : Form
        {
            private System.Windows.Forms.PictureBox pictureBox1 = new System.Windows.Forms.PictureBox();
            private System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
    
            Form2 _form2 = null;
            public Form1()
            {
                InitializeComponent();
    
                this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
    
                //Init usually done by the designer
                // 
                // pictureBox1
                // 
                this.pictureBox1.Location = new System.Drawing.Point(25, 13);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new System.Drawing.Size(171, 133);
                this.pictureBox1.TabIndex = 0;
                this.pictureBox1.TabStop = false;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(154, 203);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                this.ClientSize = new System.Drawing.Size(284, 262);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.pictureBox1);
                this.Load +=new EventHandler(Form1_Load);
            }
    
            void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (this.pictureBox1.Image != null)
                    this.pictureBox1.Image.Dispose();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //add a valid image here...
                this.pictureBox1.Image = Properties.Resources._71f_tgi;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (_form2 == null || _form2.IsDisposed)
                    _form2 = new Form2();
    
                _form2.Show();
    
                _form2.AddPicture(this.pictureBox1.Image);
            }
        }

    Form2:

        public partial class Form2 : Form
        {
            private System.Windows.Forms.PictureBox pictureBox1 = new System.Windows.Forms.PictureBox();
    
            public Form2()
            {
                InitializeComponent();
                this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
    
                // 
                // pictureBox1
                // 
                this.pictureBox1.Location = new System.Drawing.Point(12, 12);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new System.Drawing.Size(260, 166);
                this.pictureBox1.TabIndex = 0;
                this.pictureBox1.TabStop = false;
                this.ClientSize = new System.Drawing.Size(284, 262);
                this.Controls.Add(this.pictureBox1);
            }
    
            void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (this.pictureBox1.Image != null)
                    this.pictureBox1.Image.Dispose();
            }
    
            internal void AddPicture(Image img)
            {
                Image iOld = this.pictureBox1.Image;
                this.pictureBox1.Image = (Image)img.Clone();
    
                if (iOld != null)
                    iOld.Dispose();
            }
        }

    Regards,

      Thorsten

    • Marked as answer by Angry_Ant Wednesday, August 22, 2012 1:25 PM
    Tuesday, August 21, 2012 10:40 PM
  • This worked great!

    Thanks,

    Ant

    Wednesday, August 22, 2012 1:25 PM