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!
class WebCamera : IDisposable
{
#region Constants
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
[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);
[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);
[DllImport("user32")]
protected static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[DllImport("user32", EntryPoint = "SendMessageA")]
protected static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
[DllImport("user32")]
protected static extern bool DestroyWindow(int hwnd);
int DeviceID = 0;
int hHwnd = 0;
ArrayList ListOfDevices = new ArrayList();
public PictureBox Container { get; set; }
#endregion
#region Load Method
public void Load()
{
string Name = String.Empty.PadRight(100);
string Version = String.Empty.PadRight(100);
bool EndOfDeviceList = false;
short index = 0;
do
{
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);
IntPtr oHandle = Container.Handle;
{
MessageBox.Show("Set the container property");
}
hHwnd = capCreateCaptureWindowA(ref DeviceIndex, WS_VISIBLE | WS_CHILD, 0, 0, 640, 480, oHandle.ToInt32(), 0);
if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, DeviceID, 0) != 0)
{
SendMessage(hHwnd, WM_CAP_SET_SCALE, -1, 0);
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, -1, 0);
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, Container.Height, Container.Width, SWP_NOMOVE | SWP_NOZORDER);
}
else
{
DestroyWindow(hHwnd);
}
}
#endregion
#region Close Connection
public void CloseConnection()
{
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, DeviceID, 0);
DestroyWindow(hHwnd);
}
#endregion
#region Save Image
public void SaveImage()
{
IDataObject data;
Image oImage;
SaveFileDialog saveImage = new SaveFileDialog();
saveImage.Filter = "(*.bmp)|*.bmp";
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0);
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
public void Dispose()
{
CloseConnection();
}
#endregion
}