Search code examples
c#winformscamera

How to use camera capture event of a form in another form?


I am creating a new UI and I want to use the camera capturing event in the form1 on form2(in short, i am trying to transfer data from pictureBox1 of Form1 to pictureBox1 of Form2.) How can i achieve that?

Thanks...


Solution

    1. In form 2, implement a public method to accept an image and display in the picture box
    2. In form 1, in the PAINT event of the picture box, call the above method of Form 2

    Sample code

    Form 1

    public partial class Form1 : Form
    {
        //Object of Form 2 in Form 1
        Form2 oFrm2 = new Form2();
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Load an image in the picture box
            pictureBox1.Load(@"E:\Temp\SmartRetire.jpeg");
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            //Consume the PAINT event of the picture box to notify a change in image
            pictureBox1.Paint += PictureBox1_Paint;
            //Show a blank form2
            oFrm2.Show();
        }
    
        //Raised when the picture box image is changed
        private void PictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //Show the image in form 2
            oFrm2.ShowImage(pictureBox1.Image);
        }
    }
    

    Form 2

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        //Public method called from Form1 when the image is changed in picture box (in form1)
        public void ShowImage(Image oImage)
        {
            //Display the picture in form2
            pictureBox1.Image = oImage;
        }
    
    }