public ActionResult Clear(int id, string name)
        {
            List<String> files = new List<String>();
            files.Add(_hostEnvironment.WebRootPath + "\\static\\Camera\\" + id + "\\" + name + ".png");
            files.Add(_hostEnvironment.WebRootPath + "\\static\\Camera\\" + id + "\\diff\\" + name + ".png");
            files.Add(_hostEnvironment.WebRootPath + "\\static\\Camera\\" + id + "\\left\\" + name + ".png");
            files.Add(_hostEnvironment.WebRootPath + "\\static\\Camera\\" + id + "\\right\\" + name + ".png");
            files.Add(_hostEnvironment.WebRootPath + "\\static\\Camera\\" + id + "\\last\\" + name + ".png");

            foreach (String file in files)
            {
                try
                {
                    System.IO.File.Delete(file);
                }
                catch
                {
                    // void
                }
            }

            CameraService.DPP.TemplateMessage result = new DPP.TemplateMessage("result");
            result.id = CameraService.DPP.TemplateMessage.NextId();
            result.result = "OK";
            return Content(result.stringify().ToString(), "application/json");
        }
        public ActionResult Compare(int id, string name, string d, int th = 60)
        {
            String filePath = _hostEnvironment.WebRootPath + "\\static\\Camera\\" + id;
            String filePathDiff = _hostEnvironment.WebRootPath + "\\static\\Camera\\" + id + "\\diff";
            String filePathLeft = _hostEnvironment.WebRootPath + "\\static\\Camera\\" + id + "\\left";
            String filePathRight = _hostEnvironment.WebRootPath + "\\static\\Camera\\" + id + "\\right";
            String filePathLast = _hostEnvironment.WebRootPath + "\\static\\Camera\\" + id + "\\last";

            List<String> files = new List<String>();
            files.Add(filePath);
            files.Add(filePathDiff);
            files.Add(filePathLeft);
            files.Add(filePathRight);
            files.Add(filePathLast);

            foreach(String f in files)
            {
                if (!System.IO.Directory.Exists(f))
                {
                    System.IO.Directory.CreateDirectory(f);
                }
            }

            String fileName = name + ".png";
            String fullPath = System.IO.Path.Combine(filePathLast, fileName);

            Bitmap right = null;
            try
            {
                right = Camera.AForgeStillWrapper.GetFrame(id).Result;
                right.Save(fullPath);
            }
            catch (Exception e)
            {
                CameraService.DPP.TemplateMessage errmsg = new DPP.TemplateMessage("result");
                errmsg.id = CameraService.DPP.TemplateMessage.NextId();
                errmsg.error = new DPP.TemplateMessage.Error("Internal");
                errmsg.error.reason = "Could not get frame from camera";
                errmsg.error.offendingMessage = e.Message;

                return Content(errmsg.stringify().ToString(), "application/json");
            }
            Bitmap left = null;
            try
            {

                //  Check for comparison picture
                fullPath = System.IO.Path.Combine(filePath, fileName);
                if (System.IO.File.Exists(fullPath))
                {
                    left = (Bitmap)Image.FromFile(fullPath);
                }
                else
                {
                    // If File does not Exists make this the reference.
                    fullPath = System.IO.Path.Combine(filePath, fileName);
                    right.Save(fullPath);
                    left = (Bitmap)right.Clone();
                }
            }
            catch (Exception e)
            {
                CameraService.DPP.TemplateMessage errmsg = new DPP.TemplateMessage("result");
                errmsg.id = CameraService.DPP.TemplateMessage.NextId();
                errmsg.error = new DPP.TemplateMessage.Error("Internal");
                errmsg.error.reason = "Could not get comparison left frame";
                errmsg.error.offendingMessage = e.Message;

                return Content(errmsg.stringify().ToString(), "application/json");
            }

            // Possible crop.
            if (!String.IsNullOrEmpty(d))
            {
                try
                {
                    int x = 0;
                    int y = 0;
                    int widht = 640;
                    int height = 480;
                    //   widht x height + x + y
                    // d=165x280+85+90
                    // [165x280][85][90]
                    d = d.Replace(' ', '+');
                    String[] tmp = d.Split('+');

                    x = Int32.Parse(tmp[1]);
                    y = Int32.Parse(tmp[2]);

                    // [165][280]
                    String[] dimemsions = tmp[0].Split('x');
                    widht = Int32.Parse(dimemsions[0]);
                    height = Int32.Parse(dimemsions[1]);

                    System.Drawing.Rectangle cropArea = new Rectangle(x, y, widht, height);

                    // Crop left
                    Bitmap nleft = new Bitmap(cropArea.Width, cropArea.Height);
                    Graphics gr = Graphics.FromImage(nleft);
                    gr.DrawImage(left, -cropArea.X, -cropArea.Y);
                    gr.Dispose();
                    left.Dispose();
                    left = nleft;

                    // Crop right
                    Bitmap nright = new Bitmap(cropArea.Width, cropArea.Height);
                    gr = Graphics.FromImage(nright);
                    gr.DrawImage(right, -cropArea.X, -cropArea.Y);
                    gr.Dispose();
                    right.Dispose();
                    right = nright;
                }
                catch (Exception e)
                {
                    CameraService.DPP.TemplateMessage errmsg = new DPP.TemplateMessage("result");
                    errmsg.id = CameraService.DPP.TemplateMessage.NextId();
                    errmsg.error = new DPP.TemplateMessage.Error("Internal");
                    errmsg.error.reason = "Cropping failed with the parameter d = " + d;
                    errmsg.error.offendingMessage = e.Message;

                    return Content(errmsg.stringify().ToString(), "application/json");

                }
            }
            Bitmap resultImage = null;
            AForge.Imaging.Filters.ThresholdedDifference filter = null;
            try
            {
                fullPath = System.IO.Path.Combine(filePathLeft, fileName);
                left.Save(fullPath);

                fullPath = System.IO.Path.Combine(filePathRight, fileName);
                right.Save(fullPath);

                // Compare
                filter = new AForge.Imaging.Filters.ThresholdedDifference(th);
                filter.OverlayImage = left;
                resultImage = filter.Apply(right);

                // Safe resultImage to diff folder
                fullPath = System.IO.Path.Combine(filePathDiff, fileName);
                resultImage.Save(fullPath);
            }
            catch (Exception e)
            {
                CameraService.DPP.TemplateMessage errmsg = new DPP.TemplateMessage("result");
                errmsg.id = CameraService.DPP.TemplateMessage.NextId();
                errmsg.error = new DPP.TemplateMessage.Error("Internal");
                errmsg.error.reason = "Something went wron comparing or saving results";
                errmsg.error.offendingMessage = e.Message;

                return Content(errmsg.stringify().ToString(), "application/json");
            }

            // Dispose the images
            left.Dispose();
            right.Dispose();
            resultImage.Dispose();

            CameraService.DPP.TemplateMessage result = new DPP.TemplateMessage("result");
            result.id = CameraService.DPP.TemplateMessage.NextId();
            result.result = filter.WhitePixelsCount;

            return Content(result.stringify().ToString(), "application/json");
        }
        public ActionResult SetVideoResolutionProfile(int id, int profile)
        {
            CameraService.DPP.TemplateMessage result = new DPP.TemplateMessage("result");
            result.id = CameraService.DPP.TemplateMessage.NextId();
            result.result = Camera.AForgeStillWrapper.SetVideoResolutionProfile(id, profile);

            return Content(result.stringify(), "application/json");
        }
        public ActionResult SetCameraControlProperties(int id, String Pan, String Tilt, String Roll, String Zoom, String Exposure, String Iris, String Focus)
        {
            List<Camera.AForgeStillWrapper.ContainerForCameraControlPropery> output = new List<Camera.AForgeStillWrapper.ContainerForCameraControlPropery>();
            int pan = 0;
            if(!String.IsNullOrEmpty(Pan) && Int32.TryParse(Pan, out pan))
            {
                // Got Pan
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyManual(id, AForge.Video.DirectShow.CameraControlProperty.Pan, pan));
            }
            else if(!String.IsNullOrEmpty(Pan) && Pan.ToLower().Equals("auto"))
            {
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyAuto(id, AForge.Video.DirectShow.CameraControlProperty.Pan));
            }

            int tilt = 0;
            if (!String.IsNullOrEmpty(Tilt) && Int32.TryParse(Tilt, out tilt))
            {
                // Got Tilt
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyManual(id, AForge.Video.DirectShow.CameraControlProperty.Tilt, tilt));
            }
            else if (!String.IsNullOrEmpty(Tilt) && Tilt.ToLower().Equals("auto"))
            {
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyAuto(id, AForge.Video.DirectShow.CameraControlProperty.Tilt));
            }

            int roll = 0;
            if (!String.IsNullOrEmpty(Roll) && Int32.TryParse(Roll, out roll))
            {
                // Got Roll
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyManual(id, AForge.Video.DirectShow.CameraControlProperty.Roll, roll));
            }
            else if (!String.IsNullOrEmpty(Roll) && Roll.ToLower().Equals("auto"))
            {
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyAuto(id, AForge.Video.DirectShow.CameraControlProperty.Roll));
            }

            int zoom = 0;
            if (!String.IsNullOrEmpty(Zoom) && Int32.TryParse(Zoom, out zoom))
            {
                // Got Zoom
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyManual(id, AForge.Video.DirectShow.CameraControlProperty.Zoom, zoom));

            }
            else if (!String.IsNullOrEmpty(Zoom) && Zoom.ToLower().Equals("auto"))
            {
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyAuto(id, AForge.Video.DirectShow.CameraControlProperty.Zoom));
            }

            int exposure = 0;
            if (!String.IsNullOrEmpty(Exposure) && Int32.TryParse(Exposure, out exposure))
            {
                // Got Exposure
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyManual(id, AForge.Video.DirectShow.CameraControlProperty.Exposure, exposure));
            }
            else if (!String.IsNullOrEmpty(Exposure) && Exposure.ToLower().Equals("auto"))
            {
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyAuto(id, AForge.Video.DirectShow.CameraControlProperty.Exposure));
            }

            int iris = 0;
            if (!String.IsNullOrEmpty(Iris) && Int32.TryParse(Iris, out iris))
            {
                // Got Iris
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyManual(id, AForge.Video.DirectShow.CameraControlProperty.Iris, iris));
            }
            else if (!String.IsNullOrEmpty(Iris) && Iris.ToLower().Equals("auto"))
            {
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyAuto(id, AForge.Video.DirectShow.CameraControlProperty.Iris));
            }

            int focus = 0;
            if (!String.IsNullOrEmpty(Focus) && Int32.TryParse(Focus, out focus))
            {
                // Got Focus
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyManual(id, AForge.Video.DirectShow.CameraControlProperty.Focus, focus));
            }
            else if (!String.IsNullOrEmpty(Focus) && Focus.ToLower().Equals("auto"))
            {
                output.Add(Camera.AForgeStillWrapper.SetCameraControlPropertyAuto(id, AForge.Video.DirectShow.CameraControlProperty.Focus));
            }
            CameraService.DPP.TemplateMessage result = new DPP.TemplateMessage("result");
            result.id = CameraService.DPP.TemplateMessage.NextId();
            result.result = output;

            return Content(result.stringify(), "application/json");
        }
        public ActionResult SaveLatestAs(int id, string name)
        {
            CameraService.DPP.TemplateMessage result = new DPP.TemplateMessage("result");
            result.id = CameraService.DPP.TemplateMessage.NextId();

            String last = _hostEnvironment.WebRootPath + "\\static\\Camera\\" + id + "\\last\\" + name + ".png";
            String target = _hostEnvironment.WebRootPath + "\\static\\Camera\\" + id + "\\" + name + ".png";

            try
            {
                System.IO.File.Copy(last, target, true);
            }
            catch (Exception e)
            {
                result.error = new DPP.TemplateMessage.Error("fs");
                result.error.reason = "Failed to copy last as latest";
                result.error.offendingMessage = e.Message;
            }
            result.result = "OK";
            return Content(result.stringify().ToString(), "application/json");
        }
        public ActionResult GetFresh(int id)
        {
            try
            {
                System.Drawing.Bitmap frame = Camera.AForgeStillWrapper.GetFrame(id).Result;

                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {
                    frame.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    frame.Dispose();
                    return File(stream.ToArray(), "image/jpeg");
                }
            }
            catch (Exception e)
            {
                CameraService.DPP.TemplateMessage errmsg = new DPP.TemplateMessage("result");
                errmsg.id = CameraService.DPP.TemplateMessage.NextId();
                errmsg.error = new DPP.TemplateMessage.Error("Internal");
                errmsg.error.reason = "Could not get frame from camera";
                errmsg.error.offendingMessage = e.ToString();

                return Content(errmsg.stringify().ToString(), "application/json");
            }
        }
        public ActionResult GetDevices()
        {
            CameraService.DPP.TemplateMessage result = new DPP.TemplateMessage("result");
            result.id = CameraService.DPP.TemplateMessage.NextId();
            result.result = Camera.AForgeStillWrapper.ListDevices();

            return Content(result.stringify(), "application/json");
        }
        public ActionResult GetCameraControlProperties(int id)
        {
            CameraService.DPP.TemplateMessage result = new DPP.TemplateMessage("result");
            result.id = CameraService.DPP.TemplateMessage.NextId();
            result.result = Camera.AForgeStillWrapper.GetCameraControlProperties(id);

            return Content(result.stringify(), "application/json");
        }