Ok I Wrote this class to get to grips with the .NET enviroment so the coding may not be the best but it works. I've also attatached a project which uses the class its all simple enough
The class will make it easy for your app to view a webcamera set FPS etc.
Ok the class,
First 3 variables which can be changed within the class are,
Code:
Private CamFrameRate As Integer = 15
Private OutputHeight As Integer = 240
Private OutputWidth As Integer = 360
CamFrameRate - Starting frame rate how much of a gap there is between frames 15ms (About 65 FPS) NOTE - You can changes the FPS through a sub this is just the initial frame rate.
OutputHeight/OutputWidth - Fairly self explanitory, just sets the dimensions of output.
VB Code:
Public iRunning As Boolean
This can be called at anytime and will return if the camera is running or not
VB Code:
Messagebox.show Mycam.Irunning
How To Use The Class
VB Code:
Private myCam As iCam
Set myCam = New iCam
From here you can call a range of functions using the syntax,
VB Code:
mycam.[Function Name]
Functions
initCam(ByVal parentH As Integer)
- This is where it all starts you must call this to set the camera up first
ParentH is where we want to prievew, so if we have a pictureBox on our form, named picoutput.
VB Code:
myCam.initCam(Me.picOutput.Handle.ToInt32)
resetCam()
- If you need to reset the camera call this function, you most proberly wont but its here incase you do
VB Code:
MyCam.ResetCam()
setFrameRate(ByVal iRate As Long)
- Here you can set the frame rate by passing FPS it will then be converted into how much time between frames.
VB Code:
myCam.setFrameRate(25)
closeCam()
- Allways call when closing the application, just clears things up.
VB Code:
myCam.CloseCam()
copyFrame(ByVal src As PictureBox, ByVal rect As RectangleF)
- This sub returns an image of the current frame. You need to pass to it the source picture box (Where the camera image is) and then a rectangle specifying size dimensions.
VB Code:
Me.picStill.Image = myCam.copyFrame(Me.picOutput, New RectangleF(0, 0, _
Me.picOutput.Width, Me.picOutput.Height))
FPS()
- This sub returns the current FPS
VB Code:
MessageBox.Show MyCam.Fps()
As I say code may not be great but it works good, and i just hope sompeople may find it useful. I'd appriciate people not replying here but Pm'ing me with any problems since i'd like to keep this thread as clean as possible!
OK, this is my conversion, it's in 2005. The only difference I think is the Running property as 2003 doesn't support Public and Friend property with the same name.
VB Code:
Imports System.Windows.Forms
Imports System.Drawing
Public Class WebCamera
#Region "Api/constants"
Private Const WS_CHILD As Integer = &H40000000
Private Const WS_VISIBLE As Integer = &H10000000
Private Const SWP_NOMOVE As Short = &H2S
Private Const SWP_NOZORDER As Short = &H4S
Private Const WM_USER As Short = &H400S
Private Const WM_CAP_DRIVER_CONNECT As Integer = WM_USER + 10
Private Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_USER + 11
Private Const WM_CAP_SET_VIDEOFORMAT As Integer = WM_USER + 45
Private Const WM_CAP_SET_PREVIEW As Integer = WM_USER + 50
Private Const WM_CAP_SET_PREVIEWRATE As Integer = WM_USER + 52
Private Const WM_CAP_GET_FRAME As Long = 1084
Private Const WM_CAP_COPY As Long = 1054
Private Const WM_CAP_START As Long = WM_USER
Private Const WM_CAP_STOP As Long = (WM_CAP_START + 68)
Private Const WM_CAP_SEQUENCE As Long = (WM_CAP_START + 62)
Private Const WM_CAP_SET_SEQUENCE_SETUP As Long = (WM_CAP_START + 64)
Private Const WM_CAP_FILE_SET_CAPTURE_FILEA As Long = (WM_CAP_START + 20)
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Short, ByVal lParam As String) As Integer
Private Declare Function capCreateCaptureWindowA Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Short, ByVal hWndParent As Integer, ByVal nID As Integer) As Integer
Private Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, ByVal cbVer As Integer) As Boolean
Now to use this code you need to do something like:
VB Code:
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
_Camera = New WebCamLib.WebCamera
_Camera.StartFeed()
End Sub
Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
If _Camera.Running Then
_Camera.EndFeed()
End If
_Camera = Nothing
End Sub
Private Sub _Camera_ImageChanged() Handles _Camera.ImageChanged
Dim NewImage As Image = _Camera.CurrentImage
'display image on form
End Sub
I haven't had time to really test this, it's merely an alpha release. As you can see you can start logging at any time by using the StartLogging sub.
The logging I hope to seperate into a seperate class that deals soley with logging.
ANyways, have a play, tell me where I have gone wrong.
Hi,
I changed WebCamViewer to get the real frame rate using the CallBack function and I get about 6 frames per second.
Can you really have a 25 frames/s ?
If another application is using the web cam and we start our application, a selection device window apears, what can I do to not see this window? (sorry for my english, is not perfect) I been waiting for a solution for this problem for like two weeks, can someone help me?
Nice code...just looking at it now.
Although he's used the VisualBasic namespace I believe, and the code in certain places is a little left to be desired Could really do with going through and tidying all the lose ends etc.
I actually cannot get it to work either...but am still working on that.
MREG_GetRoot = MREG_GetRoot.OpenSubKey(lsRegRoot, Not pbReadOnly)
End If
End If
End Function
End Module
This should be changed to:
VB Code:
Public Class MTools
Public Shared Function MREG_GetRoot(ByVal psNom As String, Optional ByVal pbReadOnly As Boolean = False) As Microsoft.Win32.RegistryKey
Dim lsRegRoot As String = (psNom.Split("\")(0)).ToUpper()
Dim regKey As Microsoft.Win32.RegistryKey
Select Case lsRegRoot
Case "HKEY_CURRENT_USER"
regKey = Microsoft.Win32.Registry.CurrentUser
Case "HKEY_LOCAL_MACHINE"
regKey = Microsoft.Win32.Registry.LocalMachine
Case "HKEY_CLASSES_ROOT"
regKey = Microsoft.Win32.Registry.ClassesRoot
Case "HKEY_CURRENT_CONFIG"
regKey = Microsoft.Win32.Registry.CurrentConfig
Case "HKEY_USERS"
regKey = Microsoft.Win32.Registry.Users
Case Else
Throw New Exception("Unknown root : " & psNom)
End Select
If psNom.Contains("\") Then
lsRegRoot = psNom.Substring(psNom.IndexOf("\"))
If lsRegRoot.Length > 0 Then
regKey = regKey.OpenSubKey(lsRegRoot, Not pbReadOnly)
End If
End If
return regKey
End Function
End Class
Just daft things like that throughout the entire code, makes it hard to follow. Still can't get it working on my pc. I keep getting object not set errors.
Will take a few hours to convert completely, and then a few more to iron out all the bugs.
I want to save each 10 Sekonds a new picture.
In the following function i don´t know how I can pass the new picture form the cam to bmp.
The bmp.Save function is running correctly, I already tested it.
Private Sub Save()
Me.Show()
Dim myCam As New iCam
Dim i As Integer = 0
Dim numerOfPicture As String
Dim bmp As Bitmap
numerOfPicture = i.ToString
bmp = '**********this line is the problem**********
bmp.Save("C:\Pictures\Picture" + numerOfPicture + ".bmp")
i = i + 1
Me.Close()
End Sub
I tried to use the origin code by change it a little, but I got a big Problem.
The following changes work correctly if I click the button every 10 Sekonds.
Now the problem: If I let the Button click by software, in the pictureBox is no longer the picture but a part of the
desktop which is seen right behind the pictureBox.
Private myCam As iCam
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.picOutput.SizeMode = PictureBoxSizeMode.StretchImage
myCam = New iCam
myCam.initCam(Me.picOutput.Handle.ToInt32)
End Sub
Private Sub cmdViewStill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdViewStill.Click
If myCam.iRunning Then
Dim a As FrmImage
a = New FrmImage
a.picImage.Image = myCam.copyFrame(Me.picOutput, New RectangleF(0, 0, _
Me.picOutput.Width, Me.picOutput.Height))
Me.Show()
Else
MessageBox.Show("Camera Is Not Running!")
End If
End Sub
Now I want to save the picture:
Private Sub Save()
Me.Show()
Dim myCam As New iCam
Dim i As Integer = 0
Dim numerOfPicture As String
Dim bmp As Bitmap
numerOfPicture = i.ToString
bmp = CType(picImage.Image, Bitmap)
bmp.Save("C:\Pictures\Picture" + numerOfPicture + ".bmp")
i = i + 1
Me.Close()
End Sub
and this works If I click the button who is starting Save(), but If I let the Button click by software,
in the pictureBox is no longer the picture but a part of the
desktop which is seen right behind the pictureBox.
So I thought don´t use a button but a funtion wich starts without a button to take a picture every 10 Sekonds.
-> The same result:In the pictureBox is the desktop which is seen right behind the pictureBox.
-> And in the file it is also.
Could somebody show me a line of code with wich I can save the taken picture of the cam directly in a bitmap.
Then I could simply save the bitmap.
That means to replace these 2 lines:
a.picImage.Image = myCam.copyFrame(Me.picOutput, New RectangleF(0, 0, _
Me.picOutput.Width, Me.picOutput.Height))
in something like:
Dim bmp as Bitmap
bmp = '*****here should the cam pass one picture to bmp so I can save bmp direktly*****
I downloaded the original source for VS2003 Framework 1.1 and it appeared to work fine. I wanted to use the 2005 Framework 2.0 copy that Wokawidget posted though. I put it into VS2005 and it compiled without errors. I can start and stop the webcam (the green light goes on and off) however the ImageChanged event never seems to fire even when the cam is on (I put logging in that event in the class to verify it... I also kept tabs on the _Picture variable and it doesn't appear to have anything, like the system isn't returning it).
I know the class is talking to the web cam somewhat because it will turn it on and off, but I can't see to pull the images from it. Thoughts?
Here's the example code from my form that's trying to use the class:
vb Code:
Private WithEvents _Camera As WebCamera
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
_Camera = New WebCamera
_Camera.StartFeed()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
_Camera = New WebCamera
_Camera.StartFeed()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If _Camera.Running Then
_Camera.EndFeed()
End If
End Sub
Private Sub _Camera_ImageChanged() Handles _Camera.ImageChanged
One little problem i have found, im using VS 2008 and have managed to get an application to work on my laptop which opens up a 348x270 capture window, enables to user to press "Take Photo" and save. Perfect yeah...
Well now ive tried transferring the application it to another PC i only get half of the picture box showing (capture frame) and the other half is the form behind coming through, as if the window isnt redrawing corrrectly so you dont get a proper image being viewed let alone saved? Does that make sense?
Any suggestions?
Last edited by gonkowonko; Jan 22nd, 2008 at 11:13 AM.
I took the time to register for the forum just so that I could properly thank pino for this code. it was precisely what I need for a project I'm working on. this was after trying several others that didn't work or didn't work well. thanks!
I took the time to register for the forum just so that I could properly thank pino for this code. it was precisely what I need for a project I'm working on. this was after trying several others that didn't work or didn't work well. thanks!
great webcam code, by far the best way of doing it in VB, I've added motion detection to it, pic below, this is my first attempt at motion detection so the code maybe very grude.
I will post the source later when I've cleaned it up a little.
I thought i would add the motion detection with a visual twist, you can see what motion has been picked up by the green indicator. I also added track bars which control how sensitive it is, and also the object size it responds to. Might be handy for a security thing, you can set the size to only go off to people and not a cat for example.
great webcam code, by far the best way of doing it in VB, I've added motion detection to it, pic below, this is my first attempt at motion detection so the code maybe very grude.
I will post the source later when I've cleaned it up a little.
I thought i would add the motion detection with a visual twist, you can see what motion has been picked up by the green indicator. I also added track bars which control how sensitive it is, and also the object size it responds to. Might be handy for a security thing, you can set the size to only go off to people and not a cat for example.
Kind Regards,
OuTa-SyNc
Sir i was Looking for this type of application can you please post a draft here or email me perhaps. I need this for my thesis im gonna add sms features to it and post it here. please please please .....
ignore the commented out lines, these are for a project I'm working on. Like I said the method I used may be grude but it works...and at the same speed as the webcam. It uses 2 timers, each is set with an interval the same as the frame rate (i.e. if the cam is set to 30 FPS then the timer interval would be 33) but I changed it to 30.
If anyone can improve it please post back, as I would like an optimised version myself
does anyone know how I can hide the cam picturebox and display only the output produced from the motion detection?
If I set the cam picturebox to visible = false nothing works because the camera output is not being produced, basically I want to get the frames from the camera, add my motion detection method to it and display the results in the output picturebox only. I've tried rendering over the image set in the cam picturebox but with no luck.
Not sure if anyone has noticed but if you place anything in front of the picturebox with the cam running then it is treader as output produced from the cam???
hey since you helped me haha im helping you too here i solved the problem and added a feature where you can select the device where the program will get the image.
do re upload your webcam motion detection using my code here. i used copying from the clipboard and retrieving it..
hi every body this class is a develop on icam
and can capture image with no problem even when the window is minimized
no problem .with time adding .auto capturing and ......
If I was to use this class without using a picturebox to show the webcam feed all the time but rather initiate the camera and then when buttons are clicked grab a picture then how would I go about doing that?
I can only initiate the camera with a handle to a picturebox as of now
Ok so I found a somewhat workable solution to my own problem.
As wrote if I placed the picturebox outside the form the GrabIt picture is black also if I make the picturebox 0,0 in size the picture would be black.
So what I did was to leave the picturebox at 1,1 in size and place it somewhere on the main form where it is unnoticed and I can now GrabIt and get perfect webcam shot when buttons are clicked.
ignore the commented out lines, these are for a project I'm working on. Like I said the method I used may be grude but it works...and at the same speed as the webcam. It uses 2 timers, each is set with an interval the same as the frame rate (i.e. if the cam is set to 30 FPS then the timer interval would be 33) but I changed it to 30.
If anyone can improve it please post back, as I would like an optimised version myself
Can someone please re-attach the BBIC.zip? The website is no-longer.
This is simpe webcam code. You create simple user interface with startrecord, stoprecord, and stopcam, one listbox contain webcam device.
change name of button according to name in sourcecode and you can use this code to record webcam