I am trying develop a camera solution that will take snap shots of events and send them to a remote server using c#. I want to send an alert if the camera is not capturing, or disabled or mal-functioning.
I used Aforge to take the snap shots like this
#region AFORGE ORIGINAL
/// <summary>
/// This section starts the camera software by way of initialization
/// </summary>
private void StartCameras()
{
InitializeCameras();
//create first video source
VideoCaptureDevice videoSource1 = new VideoCaptureDevice(videoDevices[int.Parse(System.Configuration.ConfigurationManager.AppSettings["VideoSourceIndex"].ToString())].MonikerString);
videoSource1.DesiredFrameRate = 10;
videoSourcePlayer1.VideoSource = videoSource1;
videoSourcePlayer1.Start();
}
/// <summary>
/// this method is used to stop the camera
/// </summary>
private void StopCameras()
{
videoSourcePlayer1.SignalToStop();
//videoSourcePlayer1.WaitForStop();
}
/// <summary>
/// Camera initialisations
/// </summary>
private void InitializeCameras()
{
cameraNames = new ArrayList();
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)
{
throw new Exception();
}
for (int i = 1, n = videoDevices.Count; i <= n; i++)
{
string cameraName = i + " : " + videoDevices[i - 1].Name;
cameraNames.Add(cameraName);
System.IO.File.AppendAllText(Application.StartupPath + "\\Cams.txt", DateTime.Now.ToString() + " " + cameraName + "\r\n");
}
//check cameras count
if (videoDevices.Count == 1)
{
cameraNames.Add("Only one camera found");
}
else
{
}
}
catch
{
cameraNames.Add("No camers found");
}
}
/// <summary>
/// Frame capture of images
/// </summary>
/// <param name="sender"></param>
/// <param name="image"></param>
private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
{
Bitmap _image = image;
Date = DateTime.Now.Day.ToString("D2") + DateTime.Now.Month.ToString("D2") + DateTime.Now.Year.ToString();
string ImageNameAndPath;
try
{
frameCount++;
if (frameCount > int.Parse(ConfigurationManager.AppSettings["FrameCount"].ToString()))
{
string strYear = "", strMonth = "", strDay = "";
strYear = DateTime.Now.Year.ToString().Substring(2);
strMonth = DateTime.Now.Month.ToString().PadLeft(2, '0');
strDay = DateTime.Now.Day.ToString().PadLeft(2, '0');
string buildPath = strYear + "\\" + strMonth + "\\" + strDay;
if (!Directory.Exists(ImagePath + "\\" + buildPath))
Directory.CreateDirectory(ImagePath + "\\" + buildPath);
ImageNameAndPath = ImagePath + "\\" + buildPath + "\\" + m_getIP + "_" + pan + "_DB PLC " + ConfigurationManager.AppSettings["ATMID"].ToString() + ".jpg";
StopCameras();
string[] splitPan = pan.Split('_');
if (splitPan.Length >= 4)
{
//Write text to the Image
using (Graphics g = Graphics.FromImage(_image))
{
string[] colours = ConfigurationManager.AppSettings["RGBWaterMarkColour"].ToString().Split(',');
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(int.Parse(colours[0]), int.Parse(colours[1]), int.Parse(colours[2])));
FontFamily family = new FontFamily("Verdana");
Font font = new Font(Font.FontFamily, 10, FontStyle.Bold, GraphicsUnit.Pixel);
PointF location = new PointF(3, 3);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawString(splitPan[0] + " " + splitPan[1] + " DB PLC" + "\r\n" + splitPan[2] + " " + splitPan[3] + " " + ConfigurationManager.AppSettings["ATMName"].ToString(), font, solidBrush, location);
ImageNameAndPath = ImageNameAndPath.Replace(":", "");
//image.Save(ImageNameAndPath, ImageFormat.Jpeg);
image.Save(ImagePath + "\\" + buildPath + "\\" + ImageNameAndPath.Substring(ImageNameAndPath.LastIndexOf("\\") + 1), ImageFormat.Jpeg);
}
log += FileSystem.LogBuilder("CR2CAM", string.Format("{0} successfully snapped and saved.\r\n", ImageNameAndPath.Substring(ImageNameAndPath.LastIndexOf("\\") + 1)));
if (!string.IsNullOrEmpty(logPath))
FileSystem.Save(logPath, string.Format("cr2camLog{0}.log", Date), log);
}
frameCount = 0;
}
}
catch (Exception ex)
{
WriteToEventLog(ex.Message + "\r\n" + ex.StackTrace + "\r\n" + "Capture Event");
log += FileSystem.LogBuilder("CR2CAM", string.Format("Camera Error due to {0}.\r\n", ex.Message));
if (!string.IsNullOrEmpty(logPath))
FileSystem.Save(logPath, string.Format("cr2camLog{0}.log", Date), log);
connection.Close();
//Application.Restart();
}
}
#endregion