using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Xml;
using TSTExplorerDesktop;
namespace TSTExplorer
{
class CapturePicture
{
XmlDocument lpicxmldoc = new XmlDocument();
ArrayList Devices = new ArrayList();
const short WM_CAP = 0x400;
const int WM_CAP_DRIVER_CONNECT = WM_CAP + 10;
const int WM_CAP_DRIVER_DISCONNECT = WM_CAP + 11;
const int WM_CAP_EDIT_COPY = WM_CAP + 30;
const int WS_CHILD = 0x40000000;
const int WS_VISIBLE = 0x10000000;
int Height = 480;
int Width = 640;
[DllImport("user32", EntryPoint = "SendMessageA")]
protected static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
[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("avicap32.dll")]
protected static extern bool capGetDriverDescriptionA(short wDriverIndex, [MarshalAs(UnmanagedType.VBByRefStr)]ref String lpszName,int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref String lpszVer, int cbVer);
public void New()
{
mLoadDeviceList();
}
public void mLoadDeviceList()
{
string lsName = String.Empty.PadRight(100);
string lsVers = String.Empty.PadRight(100);
bool lbReturn = false;
short x = 0;
do
{
lbReturn = capGetDriverDescriptionA(x , ref lsName, 100, ref lsVers, 100);
if (lbReturn)
Devices.Add(lsName.Trim());
x += 1;
} while (!(lbReturn == false));
}
public void TakePicture()
{
int i;
for (i = 0; i <= this.Devices.Count - 1; i++)
{
string lsFilename = Login.logdir + "\\images\\" + Login.luserid + "_" + BuildLog.curtime + ".png";
TakePicture(i, lsFilename);
}
}
public void TakePicture(int iDevice)
{
string filename = Login.logdir + "\\images\\" + Login.luserid + "_" + BuildLog.curtime + ".png" ;
mLoadDeviceList();
this.TakePicture(iDevice, filename);
}
public void TakePicture(int iDevice, string filename)
{
string tmpiDevice = Convert.ToString(iDevice);
int lhHwnd = 0;
int i;
using (System.Windows.Forms.Form loWindow = new System.Windows.Forms.Form())
{
lhHwnd = capCreateCaptureWindowA(ref tmpiDevice, WS_VISIBLE | WS_CHILD, 0, 0, this.Width, this.Height, loWindow.Handle.ToInt32(), 0);
SendMessage(lhHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0);
for (i = 1; i <= 10; i++)
{
Application.DoEvents();
}
SendMessage(lhHwnd, WM_CAP_EDIT_COPY, 0, 0);
IDataObject loData = Clipboard.GetDataObject();
if (loData.GetDataPresent(typeof(System.Drawing.Bitmap)))
{
using (Image loBitmap = (Image)loData.GetData(typeof(System.Drawing.Bitmap)))
{
loBitmap.Save(filename,System.Drawing.Imaging.ImageFormat.Png );
}
}
}
}
}
}
The above code is working only for 1 shot of picture. But I want to capture picture every 1 minute. I am unable to work it out. Can you help me. I tried setting time interval using timers but it failed to capture the pictures. On debugging i found that when it is called for second time there is nothing in
IDataObject loData = Clipboard.GetDataObject();
cipboard and hence it does not save any image. Please can any 1 help me even with alternative solution?