Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I wanted to know if there is a way of getting my integrated webcam to work without using an external DLL such as directshow etc... I have found some code online, but some the device id is not getting set or it requires external references. The following uses a System 32 API call but device ID remains blank, not to sure how to set it!

C#
class WebCamera : IDisposable
    {
        #region Constants
        /// <summary>
        /// Constants are used to overload unmanaged code functions
        /// Each Constant represents a state
        /// </summary>
        private const short WM_CAP = 0x400;
        private const int WM_CAP_DRIVER_CONNECT = 0x40a;
        private const int WM_CAP_DRIVER_DISCONNECT = 0x40b;
        private const int WM_CAP_EDIT_COPY = 0x41e;
        private const int WM_CAP_SET_PREVIEW = 0x432;
        private const int WM_CAP_SET_OVERLAY = 0x433;
        private const int WM_CAP_SET_PREVIEWRATE = 0x434;
        private const int WM_CAP_SET_SCALE = 0x435;
        private const int WS_CHILD = 0x40000000;
        private const int WS_VISIBLE = 0x10000000;
        private const short SWP_NOMOVE = 0x2;
        private short SWP_NOZORDER = 0x4;
        private short HWND_BOTTOM = 1;
        #endregion

        #region DLLs and properties
        /// <summary>
        /// DllImport allows you to import dlls on the system for your code to cominicate with
        /// This function enables enumerate of WebCam devices
        /// </summary>
        [DllImport("avicap32.dll")]
        protected static extern bool capGetDriverDescriptionA(short wDriverIndex,
                                                              [MarshalAs(UnmanagedType.VBByRefStr)] ref String IpszName,
                                                              int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref String IpszVer, int cbVer);
        /// <summary>
        /// DllImport allows you to import dlls on the system for your code to cominicate with
        /// Creates a window child, so that you can display it in a PictureBox
        /// </summary>
        [DllImport("avicap32.dll")]
        protected static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName,
                                                            int dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent, int nID);

        /// <summary>
        /// DllImport allows you to import dlls on the system for your code to cominicate with
        /// This function enables set changes to the size, position, and Z order of a child window
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="hWndInsertAfter"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="cx"></param>
        /// <param name="cy"></param>
        /// <param name="wFlags"></param>
        /// <returns></returns>
        [DllImport("user32")]
        protected static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);

        /// <summary>
        /// DllImport allows you to import dlls on the system for your code to cominicate with
        /// This function enables send the specified message to a window or windows
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="wMsg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("user32", EntryPoint = "SendMessageA")]
        protected static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);

        /// <summary>
        /// DllImport allows you to import dlls on the system for your code to cominicate with
        /// This function enable destroy the window child
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        [DllImport("user32")]
        protected static extern bool DestroyWindow(int hwnd);

        // Normal device ID
        int DeviceID = 0;
        // Handle Value to preview window
        int hHwnd = 0;
        // The list of devices
        ArrayList ListOfDevices = new ArrayList();
        //Display stream in PictureBox
        public PictureBox Container { get; set; }
        #endregion

        #region Load Method
        // Connects to device.
        /// <summary>
        /// This function is used to load the list of devices
        /// </summary>
        public void Load()
        {
            string Name = String.Empty.PadRight(100);
            string Version = String.Empty.PadRight(100);
            bool EndOfDeviceList = false;
            short index = 0;

            //Load and name all of available devices into ListOfDevices
            do
            {
                //Get Driver Name and Version
                EndOfDeviceList = capGetDriverDescriptionA(index, ref Name, 100, ref Version, 100);
                //
                if (EndOfDeviceList) ListOfDevices.Add(Name.Trim());
                index += 1;
            }
            while (!(EndOfDeviceList == false));
        }
        #endregion

        #region Open Connection
        public void OpenConnection()
        {
            Load();

            string DeviceIndex = Convert.ToString(DeviceID);
            // Setting the Container(PictureBox) to handle the Device
            IntPtr oHandle = Container.Handle;
            {
                MessageBox.Show("Set the container property");
            }

            // Open Preview window in picturebox .
            // Create a child window with capCreateCaptureWindowA so you can display it in a picturebox.
            hHwnd = capCreateCaptureWindowA(ref DeviceIndex, WS_VISIBLE | WS_CHILD, 0, 0, 640, 480, oHandle.ToInt32(), 0);

            // Connect to the device
            if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, DeviceID, 0) != 0)
            {
                // Set the preview scale
                SendMessage(hHwnd, WM_CAP_SET_SCALE, -1, 0);
                // Set the preview rate in terms of milliseconds
                SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);
                // Start previewing the image from the camera
                SendMessage(hHwnd, WM_CAP_SET_PREVIEW, -1, 0);
                //Resize window to fit the PictureBox
                SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, Container.Height, Container.Width, SWP_NOMOVE | SWP_NOZORDER);
            }
            else
            {
                // Close device due to connection error
                DestroyWindow(hHwnd);
            }
        }
        #endregion

        #region Close Connection
        public void CloseConnection()
        {
            SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, DeviceID, 0);
            // Close Window
            DestroyWindow(hHwnd);
        }
        #endregion

        #region Save Image
        /// <summary>
        /// Allows you to save image to local machine as type Bmp
        /// </summary>
        public void SaveImage()
        {
            IDataObject data;
            Image oImage;
            SaveFileDialog saveImage = new SaveFileDialog();
            saveImage.Filter = "(*.bmp)|*.bmp";
            // Copy Image to clipboard
            SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0);

            // Get Image from clipboard and convert it to a bitmap
            data = Clipboard.GetDataObject();
            if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
            {
                oImage = (Image)data.GetData(typeof(System.Drawing.Bitmap));
                Container.Image = oImage;
                CloseConnection();
                if (saveImage.ShowDialog() == DialogResult.OK)
                {
                    oImage.Save(saveImage.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
                }
            }
        }
        #endregion

        #region Dispose Method
        /// <summary>
        /// Closes the connection to device and dumps all remaining memory usage
        /// </summary>
        public void Dispose()
        {
            CloseConnection();
        }
        #endregion
    }
Posted
Updated 25-Oct-12 0:39am
v2

1 solution

Is there a particular reason that you do not want to use directshow?
 
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