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

Recommended Answers

All 7 Replies

I wouldnt say its the best solution. Check out my tutorial on interform control access for a more OO approach :)
Even better would be to create an event handler in your form class that detects the new frame event in the camera class and updates its own picturebox. Is the event exposed by the camera class or is it raised by a private member of the camera class?

commented: Provided me with a relevant and well written tutorial +0

Hi Ryshad

That tutorial you sent me is excellent, i shall change my code today to reflect the changes, however I am interested in what you said about using events. I have messed around with this concept but have had no success so far in implementing it.

From what I understand you can subscribe to an event in another class and perform the relevant actions..

My question is: if the new webcam frames are from the eventargs in the camera class event handler, is it still possible to get these frames in the subscribed event handler in the form class? (hope that makes sense).

below is the event handler in the camera class, perhaps you could advise me with some pseudo code the best way to implement it?

public void FinalVideoSource_NewFrame(object sender, NewFrameEventArgs EventArgs)
        {

            //Clone frame from video source and pre-process
            Bitmap newFrame = (Bitmap)EventArgs.Frame.Clone();
            
   
        }

Thanks for your help

Tom

If you are only interested in the Bitmap you extract from the EventArgs i would suggest adding a custom event to your Camera class that alerts listeners that a new bitmap is ready.
There's a good tutorial here that helped me get to grips with the concepts.
My events look something like:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //attach event handler
            Cam.NewFrameAdded += new NewFrameAddedHandler(Cam_NewFrameAdded);
        }

        void Cam_NewFrameAdded(object sender, EventArgs e)
        {
            //when new frame is added, copy it to picturebox
            pictureBox1.Image = Cam.NewFrame;
        }

        Camera Cam = new Camera();

        private void btnTest_Click(object sender, EventArgs e)
        {
            Cam.TestEvent();
        }
    }

    //create handler signature for your event
    public delegate void NewFrameAddedHandler(object sender, EventArgs e);

    class Camera
    {
        private Bitmap _newFrame;
        public Bitmap NewFrame
        {
            get { return _newFrame; }
            set
            {
                _newFrame = value;
                OnNewFrameAdded(new EventArgs());
            }
        }

        //create event
        public event NewFrameAddedHandler NewFrameAdded;
        public virtual void OnNewFrameAdded(EventArgs e)
        {
            //if there are no listeners attached to an event it will be null
            //raising an event whilst it is null results in exception.
            //perform check to ensure event is not null before raising it:
            if (NewFrameAdded != null)
                NewFrameAdded(this, e);
        }

        public void TestEvent()
        {
            Bitmap b = (Bitmap)Image.FromFile("cele.jpg");
            NewFrame = b;
        }
    }

This is a very basic implementation. The local bitmap is accessed through a property. The set method raises the event whenever the value is set. The listning class then uses the get method of the property to retrieve the value. You could take it one step further and create a custom EventArgs class that passes the BitMap to the attached event handler as e.Bitmap.
My test method is just there to load an image into the variable to trigger the event. Hope this gets you going on the right track :) Custom events are a very powerful tool to have in your bag once you get the hang of them.

I actually don't know how much to thank you, degree project back on track.

Thanks for teaching me the correct way to program such problems, everything is looking so much tidier now and I can have my camera class as a separate entity rather than it relying on the form class.

I'm very interested in making my own custom event args but that's for another day I think. :icon_cheesygrin:

Cheers once again

Tom

No worries, but if its for a degree project then i seriously recommend you read the code thoroughly. Read the msdn pages relevant to the namespaces and concepts used and ensure you understand it all. If you get asked to explain your code, you will need to know what it is doing :)

Public Sub imgsave2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgsave2.Click
Dim path As String
Static InitialSize As Size = imgsave2.Size ' Save original ctrl size

imgsave2.Size = InitialSize ' Reset the control size

With OpenFileDialog1 ' Initialize the file dialog
.Filter = "Bitmap (*.BMP)|*.bmp|Icon (*.ICO)|*.ICO|GIF (*.GIF)|*.GIF|Metafile (*.WMF)|*.WMF|JPEG (*.JPG)|*.JPG"
.FilterIndex = 5
.InitialDirectory = "D:\"
.Title = "Select Image File"
End With

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then ' File selected?
'Label10.Text = OpenFileDialog1.FileName.ToString
path = OpenFileDialog1.FileName
Me.Text = "View a Graphics Image " & path

Dim FileStream1 As System.IO.FileStream
Dim FileInfo1 As System.IO.FileInfo

FileInfo1 = New System.IO.FileInfo(path)
FileStream1 = New System.IO.FileStream(path, IO.FileMode.Open)
Dim Array1(CInt(FileInfo1.Length - 1)) As Byte
Debug.WriteLine(FileStream1.Read(Array1, 0, CInt(FileInfo1.Length)))
FileStream1.Close()

Dim String1 As String
Dim ASCIIEncoding1 As New System.Text.ASCIIEncoding()
String1 = ASCIIEncoding1.GetString(Array1)

imgsave2.Image = Image.FromFile(path)
End If

End Sub


Private Sub imgsave3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgsave3.Click
Dim path As String
Static InitialSize As Size = imgsave3.Size ' Save original ctrl size

imgsave3.Size = InitialSize ' Reset the control size

With OpenFileDialog1 ' Initialize the file dialog
.Filter = "Bitmap (*.BMP)|*.bmp|Icon (*.ICO)|*.ICO|GIF (*.GIF)|*.GIF|Metafile (*.WMF)|*.WMF|JPEG (*.JPG)|*.JPG"
.FilterIndex = 5
.InitialDirectory = "D:\"
.Title = "Select Image File"
End With

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then ' File selected?
'Label10.Text = OpenFileDialog1.FileName.ToString
path = OpenFileDialog1.FileName
Me.Text = "View a Graphics Image " & path

Dim FileStream1 As System.IO.FileStream
Dim FileInfo1 As System.IO.FileInfo

FileInfo1 = New System.IO.FileInfo(path)
FileStream1 = New System.IO.FileStream(path, IO.FileMode.Open)
Dim Array1(CInt(FileInfo1.Length - 1)) As Byte
Debug.WriteLine(FileStream1.Read(Array1, 0, CInt(FileInfo1.Length)))
FileStream1.Close()

Dim String1 As String
Dim ASCIIEncoding1 As New System.Text.ASCIIEncoding()
String1 = ASCIIEncoding1.GetString(Array1)

imgsave3.Image = Image.FromFile(path)
End If
End Sub

Private Sub imgsave4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgsave4.Click
Dim path As String
Static InitialSize As Size = imgsave3.Size ' Save original ctrl size

imgsave4.Size = InitialSize ' Reset the control size

With OpenFileDialog1 ' Initialize the file dialog
.Filter = "Bitmap (*.BMP)|*.bmp|Icon (*.ICO)|*.ICO|GIF (*.GIF)|*.GIF|Metafile (*.WMF)|*.WMF|JPEG (*.JPG)|*.JPG"
.FilterIndex = 5
.InitialDirectory = "D:\"
.Title = "Select Image File"
End With

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then ' File selected?
'Label10.Text = OpenFileDialog1.FileName.ToString
path = OpenFileDialog1.FileName
Me.Text = "View a Graphics Image " & path

Dim FileStream1 As System.IO.FileStream
Dim FileInfo1 As System.IO.FileInfo

FileInfo1 = New System.IO.FileInfo(path)
FileStream1 = New System.IO.FileStream(path, IO.FileMode.Open)
Dim Array1(CInt(FileInfo1.Length - 1)) As Byte
Debug.WriteLine(FileStream1.Read(Array1, 0, CInt(FileInfo1.Length)))
FileStream1.Close()

Dim String1 As String
Dim ASCIIEncoding1 As New System.Text.ASCIIEncoding()
String1 = ASCIIEncoding1.GetString(Array1)

imgsave4.Image = Image.FromFile(path)
End If
End Sub

Private Sub imgsave5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgsave5.Click
Dim path As String
Static InitialSize As Size = imgsave5.Size ' Save original ctrl size

imgsave5.Size = InitialSize ' Reset the control size

With OpenFileDialog1 ' Initialize the file dialog
.Filter = "Bitmap (*.BMP)|*.bmp|Icon (*.ICO)|*.ICO|GIF (*.GIF)|*.GIF|Metafile (*.WMF)|*.WMF|JPEG (*.JPG)|*.JPG"
.FilterIndex = 5
.InitialDirectory = "D:\"
.Title = "Select Image File"
End With

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then ' File selected?
'Label10.Text = OpenFileDialog1.FileName.ToString
path = OpenFileDialog1.FileName
Me.Text = "View a Graphics Image " & path

Dim FileStream1 As System.IO.FileStream
Dim FileInfo1 As System.IO.FileInfo

FileInfo1 = New System.IO.FileInfo(path)
FileStream1 = New System.IO.FileStream(path, IO.FileMode.Open)
Dim Array1(CInt(FileInfo1.Length - 1)) As Byte
Debug.WriteLine(FileStream1.Read(Array1, 0, CInt(FileInfo1.Length)))
FileStream1.Close()

Dim String1 As String
Dim ASCIIEncoding1 As New System.Text.ASCIIEncoding()
String1 = ASCIIEncoding1.GetString(Array1)

imgsave5.Image = Image.FromFile(path)
End If
End Sub

Private Sub imgsave6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgsave6.Click
Dim path As String
Static InitialSize As Size = imgsave6.Size ' Save original ctrl size

imgsave6.Size = InitialSize ' Reset the control size

With OpenFileDialog1 ' Initialize the file dialog
.Filter = "Bitmap (*.BMP)|*.bmp|Icon (*.ICO)|*.ICO|GIF (*.GIF)|*.GIF|Metafile (*.WMF)|*.WMF|JPEG (*.JPG)|*.JPG"
.FilterIndex = 5
.InitialDirectory = "D:\"
.Title = "Select Image File"
End With

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then ' File selected?
'Label10.Text = OpenFileDialog1.FileName.ToString
path = OpenFileDialog1.FileName
Me.Text = "View a Graphics Image " & path

Dim FileStream1 As System.IO.FileStream
Dim FileInfo1 As System.IO.FileInfo

FileInfo1 = New System.IO.FileInfo(path)
FileStream1 = New System.IO.FileStream(path, IO.FileMode.Open)
Dim Array1(CInt(FileInfo1.Length - 1)) As Byte
Debug.WriteLine(FileStream1.Read(Array1, 0, CInt(FileInfo1.Length)))
FileStream1.Close()

Dim String1 As String
Dim ASCIIEncoding1 As New System.Text.ASCIIEncoding()
String1 = ASCIIEncoding1.GetString(Array1)

imgsave6.Image = Image.FromFile(path)
End If
End Sub

Private Sub imgsave7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgsave7.Click
Dim path As String
Static InitialSize As Size = imgsave7.Size ' Save original ctrl size

imgsave7.Size = InitialSize ' Reset the control size

With OpenFileDialog1 ' Initialize the file dialog
.Filter = "Bitmap (*.BMP)|*.bmp|Icon (*.ICO)|*.ICO|GIF (*.GIF)|*.GIF|Metafile (*.WMF)|*.WMF|JPEG (*.JPG)|*.JPG"
.FilterIndex = 5
.InitialDirectory = "D:\"
.Title = "Select Image File"
End With

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then ' File selected?
'Label10.Text = OpenFileDialog1.FileName.ToString
path = OpenFileDialog1.FileName
Me.Text = "View a Graphics Image " & path

Dim FileStream1 As System.IO.FileStream
Dim FileInfo1 As System.IO.FileInfo

FileInfo1 = New System.IO.FileInfo(path)
FileStream1 = New System.IO.FileStream(path, IO.FileMode.Open)
Dim Array1(CInt(FileInfo1.Length - 1)) As Byte
Debug.WriteLine(FileStream1.Read(Array1, 0, CInt(FileInfo1.Length)))
FileStream1.Close()

Dim String1 As String
Dim ASCIIEncoding1 As New System.Text.ASCIIEncoding()
String1 = ASCIIEncoding1.GetString(Array1)

imgsave7.Image = Image.FromFile(path)
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'CONNECTION
If TextBox1.Text = "" Then
TextBox1.Focus()
MessageBox.Show("Please Enter Id Number")
End If
Dim OleDbConnection1 As New System.Data.OleDb.OleDbConnection("PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=D:\Testing\DATABASE\TESTING.MDB;PERSIST SECURITY INFO=FALSE;")
OleDbConnection1.Open()
Dim OleDbInsertCommand1 As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand()

'Access (OleDb)
OleDbInsertCommand1.CommandText = "INSERT INTO Locker (lockerNo, h1_img, h2_img, h3_img, h1_sign, h2_sign, h3_sign, holder1, holder2, holder3, add1, add2, add3, pin, cell, email, acno) VALUES (@lockerNo, @h1_img, @h2_img, @h3_img, @h1_sign, @h2_sign, @h3_sign, @holder1, @holder2, @holder3, @add1, @add2, @add3, @pin, @cell, @email, @acno)"
OleDbInsertCommand1.Connection = OleDbConnection1
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@lockerNo", System.Data.OleDb.OleDbType.Integer, 4, "lockerNo"))
'OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Nameb", System.Data.OleDb.OleDbType.VarChar, 80, "Nameb"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@h1_img", System.Data.OleDb.OleDbType.VarBinary, 2147483647, "h1_img"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@h2_img", System.Data.OleDb.OleDbType.VarBinary, 2147483647, "h2_img"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@h3_img", System.Data.OleDb.OleDbType.VarBinary, 2147483647, "h3_img"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@h1_sign", System.Data.OleDb.OleDbType.VarBinary, 2147483647, "h1_sign"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@h2_sign", System.Data.OleDb.OleDbType.VarBinary, 2147483647, "h2_sign"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@h3_sign", System.Data.OleDb.OleDbType.VarBinary, 2147483647, "h3_sign"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@holder1", System.Data.OleDb.OleDbType.VarChar, 100, "holder1"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@holder2", System.Data.OleDb.OleDbType.VarChar, 100, "holder2"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@holder3", System.Data.OleDb.OleDbType.VarChar, 100, "holder3"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@add1", System.Data.OleDb.OleDbType.VarChar, 100, "add1"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@add2", System.Data.OleDb.OleDbType.VarChar, 100, "add2"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@add3", System.Data.OleDb.OleDbType.VarChar, 100, "add3"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@pin", System.Data.OleDb.OleDbType.VarChar, 6, "pin"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@cell", System.Data.OleDb.OleDbType.VarChar, 100, "cell"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@email", System.Data.OleDb.OleDbType.VarChar, 100, "email"))
OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("@acno", System.Data.OleDb.OleDbType.VarChar, 11, "acno"))

Dim FileStream1 As System.IO.FileStream
Dim FileInfo1 As System.IO.FileInfo

'' READ THE FILE INTO MEMORY
'If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

' TextBox2.Text = OpenFileDialog1.FileName.ToString()
' strFileName = OpenFileDialog1.FileName.Substring(3) 'here using substring function
'End If

'FileInfo1 = New System.IO.FileInfo(TextBox2.Text)
'FileStream1 = New System.IO.FileStream(TextBox2.Text, IO.FileMode.Open)
'Dim Array1 As Byte
'Debug.WriteLine(FileStream1.Read(Array1, 0, CInt(FileInfo1.Length)))
'FileStream1.Close()

'Dim String1 As String
'Dim ASCIIEncoding1 As New System.Text.ASCIIEncoding()
'String1 = ASCIIEncoding1.GetString(Array1)

' RUN THE COMMAND
OleDbInsertCommand1.Parameters("@lockerNo").Value = TextBox1.Text
'OleDbInsertCommand1.Parameters("@Nameb").Value = strFileName
OleDbInsertCommand1.Parameters("@h1_img").Value = string1
OleDbInsertCommand1.Parameters("@h2_img").Value = string1
OleDbInsertCommand1.Parameters("@h3_img").Value = string1
OleDbInsertCommand1.Parameters("@h1_sign").Value = string1
OleDbInsertCommand1.Parameters("@h2_sign").Value = string1
OleDbInsertCommand1.Parameters("@h3_sign").Value = string1
OleDbInsertCommand1.Parameters("@holder1").Value = "hjj"
OleDbInsertCommand1.Parameters("@holder2").Value = TextBox3.Text
OleDbInsertCommand1.Parameters("@holder3").Value = TextBox4.Text
OleDbInsertCommand1.Parameters("@add1").Value = TextBox5.Text
OleDbInsertCommand1.Parameters("@add2").Value = TextBox6.Text
OleDbInsertCommand1.Parameters("@add3").Value = TextBox7.Text
OleDbInsertCommand1.Parameters("@pin").Value = TextBox8.Text
OleDbInsertCommand1.Parameters("@cell").Value = TextBox9.Text
OleDbInsertCommand1.Parameters("@email").Value = TextBox10.Text
OleDbInsertCommand1.Parameters("@acno").Value = TextBox11.Text

OleDbInsertCommand1.ExecuteNonQuery()
MsgBox("Image Successfully Stored")
OleDbConnection1.Close()
End Sub

form.PictureBox.Image = image

"Use this code instead of (PictureBox1.Image = Image.FromFile(path))"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.