For the past few weeks iv been working on a C# app to record from a webcam and make notes on it via button presses during said capture. The problem now is that by default the app freezes during capture, and stops capturing with a mouse click (you can see why thats a problem). I know that i need to change it using the captureparms structure which holds information on the default behaviour of the capture, but my attempts up to now just dont work. My sendmessage to set the capture parameters is successful and returned true when i put in code to check, so it leaves me a bit stumped and im thinking it must be something related to me doing the structure wrongly(im very new to structures... in reality theres alot of concepts involved in this that iv never seen before and iv been lucky to get by and actualy understand everything up to now). Is there any chance anyone could help shed some light on why it isnt working?
il just post all of the code, incase its something else iv missed or if anyone sees this and wants to play with webcams themselves.
HTML Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Threading; namespace WebCam { public partial class frmCamera : Form { public IntPtr webcam1; public const int WM_CAP_START = 1024; const int WS_CHILD = 1073741824; const int WS_VISIBLE = 268435456; const int WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10); const int WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11); const int WM_CAP_EDIT_COPY = (WM_CAP_START + 30); const int WM_CAP_SEQUENCE = (WM_CAP_START + 62); const int WM_CAP_FILE_SAVEAS = (WM_CAP_START + 23); const int WM_CAP_SET_SCALE = (WM_CAP_START + 53); const int WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52); const int WM_CAP_SET_PREVIEW = (WM_CAP_START + 50); const int SWP_NOMOVE = 2; const int SWP_NOSIZE = 1; const int SWP_NOZORDER = 4; const int webcam1_BOTTOM = 1; const int WM_CAP_FILE_SET_CAPTURE_FILE = (WM_CAP_START + 20); const int WM_CAP_STOP = (WM_CAP_START + 68); const int WM_CAP_SET_SEQUENCE_SETUP = (WM_CAP_START + 65); [System.Runtime.InteropServices.DllImport("avicap32.dll")] static extern bool capGetDriverDescriptionA(short wDriverIndex, string lpszName, int cbName, string lpszVer, int cbVer); [System.Runtime.InteropServices.DllImport("user32.dll")] static extern bool capCaptureSetSetup(IntPtr webcam1, CAPTUREPARMS cap, int wSize); [System.Runtime.InteropServices.DllImport("avicap32.dll")] static extern IntPtr capCreateCaptureWindow( string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int form, int nID); [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr webcam1, int Msg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam); [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SendMessageA", SetLastError = true)] public static extern int SendMessage2(IntPtr webcam1, int Msg, IntPtr wParam, ref CAPTUREPARMS lParam); [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetWindowPos")] static extern int SetWindowPos(IntPtr webcam1, int webcam1InsertAfter, int x, int y, int cx, int cy, int wFlags); [System.Runtime.InteropServices.DllImport("user32")] static extern bool DestroyWindow(IntPtr webcam1); public frmCamera() { InitializeComponent(); } private void PreviewVideo(PictureBox pbox) { webcam1 = capCreateCaptureWindow("0", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, pbox.Handle.ToInt32(), 0); CAPTUREPARMS CaptureParams = new CAPTUREPARMS(); CaptureParams.fYield = 1; CaptureParams.fAbortLeftMouse = 0; CaptureParams.fAbortRightMouse = 0; CaptureParams.dwRequestMicroSecPerFrame = 66667; CaptureParams.fMakeUserHitOKToCapture = 0; CaptureParams.wPercentDropForError = 10; CaptureParams.wChunkGranularity = 0; CaptureParams.dwIndexSize = 0; CaptureParams.wNumVideoRequested = 10; CaptureParams.fCaptureAudio = 0; CaptureParams.fMCIControl = 0; CaptureParams.fStepMCIDevice = 0; CaptureParams.dwMCIStartTime = 0; CaptureParams.dwMCIStopTime = 0; CaptureParams.fStepCaptureAt2x = 0; CaptureParams.wStepCaptureAverageFrames = 5; CaptureParams.dwAudioBufferSize = 0; if (SendMessage(webcam1, WM_CAP_DRIVER_CONNECT, 0, 0) != 0) { SendMessage(webcam1, WM_CAP_SET_SCALE, 1, 0); SendMessage(webcam1, WM_CAP_SET_PREVIEWRATE, 30, 0); SendMessage(webcam1, WM_CAP_SET_PREVIEW, 1, 0); SetWindowPos(webcam1, webcam1_BOTTOM, 0, 0, pbox.Width, pbox.Height, SWP_NOMOVE | SWP_NOZORDER); } else { DestroyWindow(webcam1); } SendMessage(webcam1, WM_CAP_FILE_SET_CAPTURE_FILE, 0, "I:\\" + System.DateTime.Now.ToFileTime() + ".avi"); SendMessage2(webcam1, WM_CAP_SET_SEQUENCE_SETUP, new IntPtr(Marshal.SizeOf(CaptureParams)), ref CaptureParams); } private void btnStop_Click(object sender, EventArgs e) { btnRecord.Enabled = true; btnStop.Enabled = false; Application.DoEvents(); SendMessage(webcam1, WM_CAP_STOP, 0, 0); } private void btnRecord_Click(object sender, EventArgs e) { btnRecord.Enabled = false; btnStop.Enabled = true; Application.DoEvents(); SendMessage(webcam1, WM_CAP_SEQUENCE, 0, 0); } private void btnSnapshot_Click(object sender, EventArgs e) { IDataObject data; Image bmap; SendMessage(webcam1, WM_CAP_EDIT_COPY, 0, 0); data = Clipboard.GetDataObject(); if (data.GetDataPresent(typeof(System.Drawing.Bitmap))) { bmap = ((Image)(data.GetData(typeof(System.Drawing.Bitmap)))); bmap.Save("I:\\" + System.DateTime.Now.ToFileTime() + ".bmp"); } } private void frmCamera_Load(object sender, EventArgs e) { PreviewVideo(picFrame); } private void btnClose_Click(object sender, EventArgs e) { Application.Exit(); } [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)] public struct CAPTUREPARMS { public System.UInt32 dwRequestMicroSecPerFrame; public System.Int32 fMakeUserHitOKToCapture; public System.UInt32 wPercentDropForError; public System.Int32 fYield; public System.UInt32 dwIndexSize; public System.UInt32 wChunkGranularity; public System.Int32 fCaptureAudio; public System.UInt32 wNumVideoRequested; public System.Int32 fAbortLeftMouse; public System.Int32 fAbortRightMouse; public System.Int32 fMCIControl; public System.Int32 fStepMCIDevice; public System.UInt32 dwMCIStartTime; public System.UInt32 dwMCIStopTime; public System.Int32 fStepCaptureAt2x; public System.UInt32 wStepCaptureAverageFrames; public System.UInt32 dwAudioBufferSize; public void SetParams(System.Int32 fYield, System.Int32 fAbortLeftMouse, System.Int32 fAbortRightMouse, System.UInt32 dwRequestMicroSecPerFrame, System.Int32 fMakeUserHitOKToCapture, System.UInt32 wPercentDropForError, System.UInt32 dwIndexSize, System.UInt32 wChunkGranularity, System.UInt32 wNumVideoRequested, System.Int32 fCaptureAudio, System.Int32 fMCIControl, System.Int32 fStepMCIDevice, System.UInt32 dwMCIStartTime, System.UInt32 dwMCIStopTime, System.Int32 fStepCaptureAt2x, System.UInt32 wStepCaptureAverageFrames, System.UInt32 dwAudioBufferSize) { this.dwRequestMicroSecPerFrame = dwRequestMicroSecPerFrame; this.fMakeUserHitOKToCapture = fMakeUserHitOKToCapture; this.fYield = fYield; this.wPercentDropForError = wPercentDropForError; this.dwIndexSize = dwIndexSize; this.wChunkGranularity = wChunkGranularity; this.wNumVideoRequested = wNumVideoRequested; this.fCaptureAudio = fCaptureAudio; this.fAbortLeftMouse = fAbortLeftMouse; this.fAbortRightMouse = fAbortRightMouse; this.fMCIControl = fMCIControl; this.fStepMCIDevice = fStepMCIDevice; this.dwMCIStartTime = dwMCIStartTime; this.dwMCIStopTime = dwMCIStopTime; this.fStepCaptureAt2x = fStepCaptureAt2x; this.wStepCaptureAverageFrames = wStepCaptureAverageFrames; this.dwAudioBufferSize = dwAudioBufferSize; } } } }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks