views:

87

answers:

6

Hi all,

I need some advice please on the best way to achieve a particular outcome...

My scenario...

I have a Form1 class, which is my main Form with a picture box on it.

I have a second class called camera that using an event handler grabs a frame (bitmap) from my webcam.

I then want to pass this frame to the picture box in the main form in the best fashion.

At the moment in the main form I have the code:

public static void setPB(Bitmap image) 
{

var form = Form.ActiveForm as Form1;
form.pbWebCamDisplay = image;

}

then in the Camera class I use

Form1.setPB(currentFrame);

This works fine, however I wasn't sure if this was best programming practice? Is it possible to use custom events?

Thanks in advance for any help

Tom

A: 

I would use events if I were in your place.

thelost
Would you be able to point me in the right direction on how to implement this please? Thanks
Tom Price
A: 

Yeah, I would say is perfectly good. You have a camera class that is in charge of handling the camera input and a public setter for a private member of your form. I would not change a bit.

That way you separate the different responsibilities for the functionalities into different classes. Don't handle the camera events inside the Form class, instead do as you're doing.

Jorge Córdoba
A: 

I would create a method on the Camera class, which takes the picture and returns a Bitmap object.

So in the OnLoad or in the OnClick of a button, call that method and set the return value to the PictureBox...

var picture = new Camera().TakePicture();
myPictureBox.Image = picture;

Something like that :)

Edit: If the Camera class is an Event driven component (Camera.NewFrame event), try:

(in the constructor):

var camera = new Camera();
camera.OnNewFrame += new OnNewFrameEventHandler(MyEventHandler);

(event handler)

public void MyEnventHandler(EventArgs comingFromComponent)
{
   var image = comingFromComponent.Frame; // (hopefully this is the case :))
   myPictureBox.Image = image;
}
Bertvan
Thanks for the reply, unfortunately I need the picture box to display a live stream. I was using a timer in the main form initially to grab the image however, the timer and new frame event handler were out of sync hence making it difficult to dispose of the bitmaps correctly.
Tom Price
In that case, can't you just subscribe to the Event from within your form? I'll update my answer...
Bertvan
that would be ideal, I wasn't aware of that feature. Would you be able to give me some sample code.
Tom Price
Thanks - I'll have a go at implementing that and let you know -
Tom Price
What (camera) class are you using exactly from the AForge.NET framework? I'll have a look, to see what I'm telling here is correct...
Bertvan
http://vimeo.com/7526663 - the video of the code i based it on is here - thought it maybe easier than explaining. Ive taken all the camera components in the video and made them into sperate class
Tom Price
A: 

Definitely, events are the right choice. You can implement Observer Design Pattern using them. Your current implementation is not best practice - it forces tight coupling between Camera and Form class.

Paweł Dyda
Hi, Would you be able to elaborate please, if you have any links that would be great. Thanks for your help
Tom Price
Here you are: http://msdn.microsoft.com/en-us/library/ee817669.aspx
Paweł Dyda
Microsoft calls Observer with events an Event Pattern, good to know.
Paweł Dyda
A: 

what drives the event that make Camera class grab the picture? This is important.

You don't even need the PictureBox setter in the form if the event is fired by the form itself.

BTW, i'm with Jorge Córdoba, a setter and a grab event are all that you need. Occam razor.

vaitrafra
So.. i'm using Aforge framework (aforgenet.com) which has it's own custom event handler which grabs the frames.
Tom Price
A: 

You can use Composition here i.e. ask yourself that Can my camera live outside of my form?

if you got answer as NO

than

Create a Property in Form class

public Camera myCamera {get;set;}

assign this property in your form constructor

public Form()

{ myCamera = new Camera (); }

and then use myCamera perperty.

another way , you can say that you are injecting your Dependency using Constructor Injection which is more advance topic of 'Dependency Injection'

saurabh