Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

Sorry a newbiw question here. Hope no one minds as its probably a simple question?.

I have searched Google and cant find the answer anywhere, but then again I maybe looking at the wrong things ....

I have the following code in my Application:
C#
void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    Bitmap bit = (Bitmap)eventArgs.Frame.Clone();
    picWebCam.Image = bit;
}

I need to call 'bit.dispose();' from 'void cam_NewFrame' but from within:
C#
private void btnStop_Click(object sender, EventArgs e)
{
    cam.Stop();
    bit.dispose(); // Need to add in here
}

Obviously this isnt working as its in a different class(?)?

How can i call bit.dispose from my btnStop?

Many Thanks
SJG
Posted
Comments
Sergey Alexandrovich Kryukov 16-Feb-13 19:20pm    
Simple questions are just fine, but they should be formulated correctly.
Is it AForge.NET? You cannot show the code without any references to types (libraries) you use.
You should always show where you add an event handler to the invocation list of the event (NewFrame?). Who knows what cam_NewFrame really is? You should always rename all auto-generated names...
Your "it's not working" is not informative at all.
—SA
sjg2004_01 17-Feb-13 6:26am    
Hi All,

OrgiginalGriff,

Many thanks for you reply. I cannot modify the cam.Class as its a pre-compiled DLL which is iported into my project..

Sergey Alexandrovich Kryukow,

You are correct. This is for AForge.Net . Both classes are on my main form. Hopefully pasteing all my code should help?

All the code for my main form is:

Collapse | Copy Code
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Xml;

namespace TestApplication
{
public partial class frmMain : Form
{
// Needed for Video Capture (AForge Video)
private FilterInfoCollection webcam;
private VideoCaptureDevice cam;

private void frmMain_Load(object sender, EventArgs e)
{
#region Video Capture Functions
webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in webcam)
{
cboSelectCamera.Items.Add(VideoCaptureDevice.Name);
}

cboSelectCamera.SelectedIndex = 0;

cam = new VideoCaptureDevice(webcam[cboSelectCamera.SelectedIndex].MonikerString);
cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
#endregion
}

private void btnSnap_Click(object sender, EventArgs e)
{
picDBImage.Image = picWebCam.Image;
}

private void btnStartStreaming_Click(object sender, EventArgs e)
{
cam = new VideoCaptureDevice(webcam[cboSelectCamera.SelectedIndex].MonikerString);
cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
cam.Start();
}

private void btnStopStreaming_Click(object sender, EventArgs e)
{
if (cam.IsRunning)
{
cam.Stop();
}
}

private void btnStart_Click(object sender, EventArgs e)
{
cam.Start();
}

private void btnStop_Click(object sender, EventArgs e)
{
cam.SignalToStop();
cam.Stop();

// Need to add in and call cam_NewFrame - bit.dispose here
}

#region Video Functions
private void btnTakePhoto_Click(object sender, EventArgs e)
{
sfdCameraImage.InitialDirectory = @"C:\";
if (sfdCameraImage.ShowDialog() == DialogResult.OK)
{
cam.Stop();
picDBImage.Image.Save(sfdCameraImage.FileName);
}
}

void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bit = (Bitmap)eventArgs.Frame.Clone();
picWebCam.Image = bit;
}
#endregion
}
}

1 solution

Why not dispose it in the cam class? It's part of the class, so disposing of it in the Stop method would make sense to me!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900