By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,030 Members | 2,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,030 IT Pros & Developers. It's quick & easy.

capturing video with a webcam

P: 4
While searching for capturing video with a webcam I god the following code.It gives two errors.Says:The type or namespace name 'WebcamEventArgs' does not exit in the namespace 'WebCam_Capture' (are you missing an assembly reference?)

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Windows.Forms;
  7. using System.Runtime.InteropServices;
  8. namespace WebCam_Capture
  9. {
  10.     /// <summary>
  11.     /// Summary description for UserControl1.
  12.     /// </summary>
  13.     [System.Drawing.ToolboxBitmap(typeof(WebCamCapture), "CAMERA.ICO")] // 
  14.     [Designer("Sytem.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(System.ComponentModel.Design.IDesigner))] // 
  15.     public class WebCamCapture : System.Windows.Forms.UserControl
  16.     {
  17.         private System.ComponentModel.IContainer components;
  18.         private System.Windows.Forms.Timer timer1;
  19.         // 
  20.         private int m_TimeToCapture_milliseconds = 100;
  21.         private int m_Width = 320;
  22.         private int m_Height = 240;
  23.         private int mCapHwnd;
  24.         private ulong m_FrameNumber = 0;
  25.         // 
  26.         private WebCam_Capture.WebcamEventArgs x = new WebCam_Capture.WebcamEventArgs();
  27.         private IDataObject tempObj;
  28.         private System.Drawing.Image tempImg;
  29.         private bool bStopped = true;
  30.         // 
  31.         public delegate void WebCamEventHandler(object source, WebCam_Capture.WebcamEventArgs e);
  32.         // 
  33.         public event WebCamEventHandler ImageCaptured;
  34.         #region API Declarations
  35.         [DllImport("user32", EntryPoint = "SendMessage")]
  36.         public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
  37.         [DllImport("avicap32.dll", EntryPoint = "capCreateCaptureWindowA")]
  38.         public static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, int hwndParent, int nID);
  39.         [DllImport("user32", EntryPoint = "OpenClipboard")]
  40.         public static extern int OpenClipboard(int hWnd);
  41.         [DllImport("user32", EntryPoint = "EmptyClipboard")]
  42.         public static extern int EmptyClipboard();
  43.         [DllImport("user32", EntryPoint = "CloseClipboard")]
  44.         public static extern int CloseClipboard();
  45.         #endregion
  46.         #region API Constants
  47.         public const int WM_USER = 1024;
  48.         public const int WM_CAP_CONNECT = 1034;
  49.         public const int WM_CAP_DISCONNECT = 1035;
  50.         public const int WM_CAP_GET_FRAME = 1084;
  51.         public const int WM_CAP_COPY = 1054;
  52.         public const int WM_CAP_START = WM_USER;
  53.         public const int WM_CAP_DLG_VIDEOFORMAT = WM_CAP_START + 41;
  54.         public const int WM_CAP_DLG_VIDEOSOURCE = WM_CAP_START + 42;
  55.         public const int WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 43;
  56.         public const int WM_CAP_GET_VIDEOFORMAT = WM_CAP_START + 44;
  57.         public const int WM_CAP_SET_VIDEOFORMAT = WM_CAP_START + 45;
  58.         public const int WM_CAP_DLG_VIDEOCOMPRESSION = WM_CAP_START + 46;
  59.         public const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
  60.         #endregion
  61.         #region NOTES
  62.  
  63.         #endregion
  64.  
  65.         public WebCamCapture()
  66.         {
  67.                     InitializeComponent();
  68.         }
  69.         /// <summary>
  70.         /// Override the class's finalize method, so we can stop
  71.         /// the video capture on exit
  72.         /// </summary>
  73.         ~WebCamCapture()
  74.         {
  75.             this.Stop();
  76.         }
  77.                 protected override void Dispose(bool disposing)
  78.         {
  79.             if (disposing)
  80.             {
  81.                 if (components != null)
  82.                     components.Dispose();
  83.             }
  84.             base.Dispose(disposing);
  85.         }
  86.         #region Component Designer generated code
  87.         /// <summary>
  88.         /// Required method for Designer support - do not modify 
  89.         /// the contents of this method with the code editor.
  90.         /// </summary>
  91.         private void InitializeComponent()
  92.         {
  93.             this.components = new System.ComponentModel.Container();
  94.             this.timer1 = new System.Windows.Forms.Timer(this.components);
  95.             this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  96.             this.Name = "WebCamCapture";
  97.             this.Size = new System.Drawing.Size(342, 252);
  98.             this.Load += new System.EventHandler(this.WebCamCapture_Load);
  99.         }
  100.         #endregion
  101.         #region Control Properties
  102.         public int TimeToCapture_milliseconds
  103.         {
  104.             get
  105.             { return m_TimeToCapture_milliseconds; }
  106.             set
  107.             { m_TimeToCapture_milliseconds = value; }
  108.         }
  109.         /// <summary>
  110.         /// The height of the video capture image
  111.         /// </summary>
  112.         public int CaptureHeight
  113.         {
  114.             get
  115.             { return m_Height; }
  116.  
  117.             set
  118.             { m_Height = value; }
  119.         }
  120.         /// <summary>
  121.         /// </summary>
  122.         public int CaptureWidth
  123.         {
  124.             get
  125.             { return m_Width; }
  126.             set
  127.             { m_Width = value; }
  128.         }
  129.         /// <summary>
  130.         /// </summary>
  131.         public ulong FrameNumber
  132.         {
  133.             get
  134.             { return m_FrameNumber; }
  135.             set
  136.             { m_FrameNumber = value; }
  137.         }
  138.         #endregion
  139.         #region Start and Stop Capture Functions
  140.         /// <summary>
  141.         /// </summary>
  142.         /// <param name="FrameNumber">the frame number to start at. 
  143.         /// Set to 0 to let the control allocate the frame number</param>
  144.         public void Start(ulong FrameNum)
  145.         {
  146.             try
  147.             {
  148.                 this.Stop();
  149.                 mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, m_Width, m_Height, this.Handle.ToInt32(), 0);
  150.                 Application.DoEvents();
  151.                 SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0);
  152.                 SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, 0, 0);
  153.                 m_FrameNumber = FrameNum;
  154.                 this.timer1.Interval = m_TimeToCapture_milliseconds;
  155.                 bStopped = false;
  156.                 this.timer1.Start();
  157.             }
  158.             catch (Exception excep)
  159.             {
  160.                 MessageBox.Show("An error ocurred while starting the video capture. Check that your webcamera is connected properly and turned on.\r\n\n" + excep.Message);
  161.                 this.Stop();
  162.             }
  163.         }
  164.         /// <summary>
  165.         /// Stops the video capture
  166.         /// </summary>
  167.         public void Stop()
  168.         {
  169.             try
  170.             {
  171.                 bStopped = true;
  172.                 this.timer1.Stop();
  173.                 Application.DoEvents();
  174.                 SendMessage(mCapHwnd, WM_CAP_DISCONNECT, 0, 0);
  175.             }
  176.             catch (Exception excep)
  177.             {             }
  178.         }
  179.         #endregion
  180.         #region Video Capture Code
  181.         /// <summary>
  182.         /// Capture the next frame from the video feed
  183.         /// </summary>
  184.         private void timer1_Tick(object sender, System.EventArgs e)
  185.         {
  186.             try
  187.             {
  188.                               this.timer1.Stop();
  189.                               SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0);
  190.                               SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0);
  191.                               if (ImageCaptured != null)
  192.                 {
  193.                                   tempObj = Clipboard.GetDataObject();
  194.                     tempImg = (System.Drawing.Bitmap)tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap);
  195.  
  196.                     x.WebCamImage = tempImg.GetThumbnailImage(m_Width, m_Height, null, System.IntPtr.Zero);
  197.                                   this.ImageCaptured(this, x);
  198.                 }
  199.                               Application.DoEvents();
  200.                 if (!bStopped)
  201.                     this.timer1.Start();
  202.             }
  203.             catch (Exception excep)
  204.             {
  205.                 MessageBox.Show("An error ocurred while capturing the video image. The video capture will now be terminated.\r\n\n" + excep.Message);
  206.                 this.Stop();             }
  207.         }
  208.         #endregion
  209.         private void WebCamCapture_Load(object sender, System.EventArgs e)
  210.         {
  211.  
  212.         }
  213.     }
  214. }
Can anybody help me?
Feb 26 '07 #1
Share this Question
Share on Google+
3 Replies


P: 1
I had the same problem. I changed the code, this works for me...

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Windows.Forms;
  7. using System.Runtime.InteropServices;
  8.  
  9. namespace WebcamWhiteboard
  10. {
  11.     /// <summary>
  12.     /// Summary description for UserControl1.
  13.     /// </summary>
  14.     [System.Drawing.ToolboxBitmap(typeof(WebCamCapture), "CAMERA.ICO")] // toolbox bitmap
  15.     [Designer("Sytem.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(System.ComponentModel.Design.IDesigner))] // make composite
  16.     public class WebCamCapture : System.Windows.Forms.UserControl
  17.     {
  18.         private System.ComponentModel.IContainer components;
  19.         private System.Windows.Forms.Timer timer1;
  20.  
  21.         // property variables
  22.         private int m_TimeToCapture_milliseconds = 100;
  23.         private int m_Width = 320;
  24.         private int m_Height = 240;
  25.         private int mCapHwnd;
  26.         private ulong m_FrameNumber = 0;
  27.  
  28.         /// <summary>
  29.         /// This was missing.
  30.         /// </summary>
  31.         private class WebcamEventArgs
  32.         {
  33.             public Image WebCamImage;
  34.         }
  35.  
  36.         // global variables to make the video capture go faster
  37.         private WebCamCapture.WebcamEventArgs x = new WebCamCapture.WebcamEventArgs();
  38.         private IDataObject tempObj;
  39.         private System.Drawing.Image tempImg;
  40.         private bool bStopped = true;
  41.  
  42.         // event delegate
  43.         public delegate void WebCamEventHandler(object source, WebCamCapture.WebcamEventArgs e);
  44.         // fired when a new image is captured
  45.         public event WebCamEventHandler ImageCaptured;
  46.  
  47.         #region API Declarations
  48.  
  49.         [DllImport("user32", EntryPoint = "SendMessage")]
  50.         public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
  51.  
  52.         [DllImport("avicap32.dll", EntryPoint = "capCreateCaptureWindowA")]
  53.         public static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, int hwndParent, int nID);
  54.  
  55.         [DllImport("user32", EntryPoint = "OpenClipboard")]
  56.         public static extern int OpenClipboard(int hWnd);
  57.  
  58.         [DllImport("user32", EntryPoint = "EmptyClipboard")]
  59.         public static extern int EmptyClipboard();
  60.  
  61.         [DllImport("user32", EntryPoint = "CloseClipboard")]
  62.         public static extern int CloseClipboard();
  63.  
  64.         #endregion
  65.  
  66.         #region API Constants
  67.  
  68.         public const int WM_USER = 1024;
  69.  
  70.         public const int WM_CAP_CONNECT = 1034;
  71.         public const int WM_CAP_DISCONNECT = 1035;
  72.         public const int WM_CAP_GET_FRAME = 1084;
  73.         public const int WM_CAP_COPY = 1054;
  74.  
  75.         public const int WM_CAP_START = WM_USER;
  76.  
  77.         public const int WM_CAP_DLG_VIDEOFORMAT = WM_CAP_START + 41;
  78.         public const int WM_CAP_DLG_VIDEOSOURCE = WM_CAP_START + 42;
  79.         public const int WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 43;
  80.         public const int WM_CAP_GET_VIDEOFORMAT = WM_CAP_START + 44;
  81.         public const int WM_CAP_SET_VIDEOFORMAT = WM_CAP_START + 45;
  82.         public const int WM_CAP_DLG_VIDEOCOMPRESSION = WM_CAP_START + 46;
  83.         public const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
  84.  
  85.         #endregion
  86.  
  87.         #region NOTES
  88.  
  89.         /*
  90.    * If you want to allow the user to change the display size and 
  91.    * color format of the video capture, call:
  92.    * SendMessage (mCapHwnd, WM_CAP_DLG_VIDEOFORMAT, 0, 0);
  93.    * You will need to requery the capture device to get the new settings
  94.   */
  95.  
  96.         #endregion
  97.  
  98.  
  99.         public WebCamCapture()
  100.         {
  101.             // This call is required by the Windows.Forms Form Designer.
  102.             InitializeComponent();
  103.         }
  104.  
  105.         /// <summary>
  106.         /// Override the class's finalize method, so we can stop
  107.         /// the video capture on exit
  108.         /// </summary>
  109.         ~WebCamCapture()
  110.         {
  111.             this.Stop();
  112.         }
  113.  
  114.         /// <summary>
  115.         /// Clean up any resources being used.
  116.         /// </summary>
  117.         protected override void Dispose(bool disposing)
  118.         {
  119.             if (disposing)
  120.             {
  121.  
  122.                 if (components != null)
  123.                     components.Dispose();
  124.             }
  125.             base.Dispose(disposing);
  126.         }
  127.  
  128.         #region Component Designer generated code
  129.         /// <summary>
  130.         /// Required method for Designer support - do not modify 
  131.         /// the contents of this method with the code editor.
  132.         /// </summary>
  133.         private void InitializeComponent()
  134.         {
  135.             this.components = new System.ComponentModel.Container();
  136.             this.timer1 = new System.Windows.Forms.Timer(this.components);
  137.             // 
  138.             // timer1
  139.             // 
  140.             this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  141.             // 
  142.             // WebCamCapture
  143.             // 
  144.             this.Name = "WebCamCapture";
  145.             this.Size = new System.Drawing.Size(342, 252);
  146.             this.Load += new System.EventHandler(this.WebCamCapture_Load);
  147.  
  148.         }
  149.         #endregion
  150.  
  151.         #region Control Properties
  152.  
  153.         /// <summary>
  154.         /// The time intervale between frame captures
  155.         /// </summary>
  156.         public int TimeToCapture_milliseconds
  157.         {
  158.             get
  159.             { return m_TimeToCapture_milliseconds; }
  160.  
  161.             set
  162.             { m_TimeToCapture_milliseconds = value; }
  163.         }
  164.  
  165.         /// <summary>
  166.         /// The height of the video capture image
  167.         /// </summary>
  168.         public int CaptureHeight
  169.         {
  170.             get
  171.             { return m_Height; }
  172.  
  173.             set
  174.             { m_Height = value; }
  175.         }
  176.  
  177.         /// <summary>
  178.         /// The width of the video capture image
  179.         /// </summary>
  180.         public int CaptureWidth
  181.         {
  182.             get
  183.             { return m_Width; }
  184.  
  185.             set
  186.             { m_Width = value; }
  187.         }
  188.  
  189.         /// <summary>
  190.         /// The sequence number to start at for the frame number. OPTIONAL
  191.         /// </summary>
  192.         public ulong FrameNumber
  193.         {
  194.             get
  195.             { return m_FrameNumber; }
  196.  
  197.             set
  198.             { m_FrameNumber = value; }
  199.         }
  200.  
  201.         #endregion
  202.  
  203.         #region Start and Stop Capture Functions
  204.  
  205.         /// <summary>
  206.         /// Starts the video capture
  207.         /// </summary>
  208.         /// <param name="FrameNumber">the frame number to start at. 
  209.         /// Set to 0 to let the control allocate the frame number</param>
  210.         public void Start(ulong FrameNum)
  211.         {
  212.             try
  213.             {
  214.                 // for safety, call stop, just in case we are already running
  215.                 this.Stop();
  216.  
  217.                 // setup a capture window
  218.                 mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, m_Width, m_Height, this.Handle.ToInt32(), 0);
  219.  
  220.                 // connect to the capture device
  221.                 Application.DoEvents();
  222.                 SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0);
  223.                 SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, 0, 0);
  224.  
  225.                 // set the frame number
  226.                 m_FrameNumber = FrameNum;
  227.  
  228.                 // set the timer information
  229.                 this.timer1.Interval = m_TimeToCapture_milliseconds;
  230.                 bStopped = false;
  231.                 this.timer1.Start();
  232.             }
  233.  
  234.             catch (Exception excep)
  235.             {
  236.                 MessageBox.Show("An error ocurred while starting the video capture. Check that your webcamera is connected properly and turned on.\r\n\n" + excep.Message);
  237.                 this.Stop();
  238.             }
  239.         }
  240.  
  241.         /// <summary>
  242.         /// Stops the video capture
  243.         /// </summary>
  244.         public void Stop()
  245.         {
  246.             try
  247.             {
  248.                 // stop the timer
  249.                 bStopped = true;
  250.                 this.timer1.Stop();
  251.  
  252.                 // disconnect from the video source
  253.                 Application.DoEvents();
  254.                 SendMessage(mCapHwnd, WM_CAP_DISCONNECT, 0, 0);
  255.             }
  256.  
  257.             catch (Exception excep)
  258.             { // don't raise an error here.
  259.             }
  260.  
  261.         }
  262.  
  263.         #endregion
  264.  
  265.         #region Video Capture Code
  266.  
  267.         /// <summary>
  268.         /// Capture the next frame from the video feed
  269.         /// </summary>
  270.         private void timer1_Tick(object sender, System.EventArgs e)
  271.         {
  272.             try
  273.             {
  274.                 // pause the timer
  275.                 this.timer1.Stop();
  276.  
  277.                 // get the next frame;
  278.                 SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0);
  279.  
  280.                 // copy the frame to the clipboard
  281.                 SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0);
  282.  
  283.                 // paste the frame into the event args image
  284.                 if (ImageCaptured != null)
  285.                 {
  286.                     // get from the clipboard
  287.                     tempObj = Clipboard.GetDataObject();
  288.                     tempImg = (System.Drawing.Bitmap)tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap);
  289.  
  290.                     /*
  291.                     * For some reason, the API is not resizing the video
  292.                     * feed to the width and height provided when the video
  293.                     * feed was started, so we must resize the image here
  294.                     */
  295.                     x.WebCamImage = tempImg.GetThumbnailImage(m_Width, m_Height, null, System.IntPtr.Zero);
  296.  
  297.                     // raise the event
  298.                     this.ImageCaptured(this, x);
  299.                 }
  300.  
  301.                 // restart the timer
  302.                 Application.DoEvents();
  303.                 if (!bStopped)
  304.                     this.timer1.Start();
  305.             }
  306.  
  307.             catch (Exception excep)
  308.             {
  309.                 MessageBox.Show("An error ocurred while capturing the video image. The video capture will now be terminated.\r\n\n" + excep.Message);
  310.                 this.Stop(); // stop the process
  311.             }
  312.         }
  313.  
  314.         #endregion
  315.  
  316.         private void WebCamCapture_Load(object sender, System.EventArgs e)
  317.         {
  318.  
  319.         }
  320.     }
  321. }
While searching for capturing video with a webcam I god the following code.It gives two errors.Says:The type or namespace name 'WebcamEventArgs' does not exit in the namespace 'WebCam_Capture' (are you missing an assembly reference?)
Can anybody help me?
Sep 7 '07 #2

Plater
Expert 5K+
P: 7,872
Very interesting, does this only work for a particular cam type or for all of them that windows recognizes?
Sep 7 '07 #3

parshupooja
100+
P: 159
it looks very nice, If you guys can post detail info or article for that, will be great
Sep 7 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.