1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Updating a picture box in one class from a different class.

Discussion in 'C#' started by tomprice0189, Sep 8, 2010.

  1. tomprice0189

    tomprice0189 New Member

    Joined:
    Sep 8, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,

    So here is my situation:

    2 classes

    1st class is a windows form created in VS2008. It has a picture box on it which i want to update with images.

    2nd class is a custom class called 'camera' which uses the AForge.Net framework to grab images from my webcam. The important part is that there is an event handler that fires everytime a new frame is got from the webcam and creates a bitmap.


    So... I need some advice on the best way of updating the picturebox in the form class with the latest frame in the camera class..

    At the moment I have a set method in the form class like so...

    public static void setPB(Bitmap image)
    {


    var form = Forms.ActiveForm as Form1;
    form.PictureBox.Image = image

    }

    this method is then called in the newframe event handler within the camera class.

    This code works fine however I would like to know if this is the best way to attack the problem?

    The other way I thought of doing it was when i create an object of the camera class, to pass my form as a parameter so that I can set the picturebox image from within the camera class itself.

    Please note: i only have 1 thread at the moment.

    Thanks in advance, much appreciated.

    Tom
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    This can be achieved with the use of "Interform Control Access"...
    let me give u an example:-

    Code:
    public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
       
              private void Form1_Load(object sender, EventArgs e)
              {
                  Form2 frm2 = new Form2(this); //pass reference to this form in constructor
              }
               
        }
    
    
          public partial class Form2 : Form
          {
              Form1 frm1;
              public Form2(Form1 callingForm)
              {
                  InitializeComponent();
                  frm1 = callingForm;  //store local reference to Form1
              }
       
              private void Form2_Load(object sender, EventArgs e)
              {
                  frm1.picturebox.Image=”path”;
              }
          }
    
    
    
    Let me know for any concerns...
     

Share This Page