Exemplo n.º 1
0
        protected static void StartScreenshot(string[] args)
        {
            if (args.Length < 1)
            {
                return;
            }

            string pipeName = args[0];

            using (System.IO.Pipes.NamedPipeServerStream pipeStream = new System.IO.Pipes.NamedPipeServerStream(pipeName))
            {
                try
                {
                    pipeStream.WaitForConnection();
                    if (pipeStream.IsConnected)
                    {
                        int    iWidth  = Screen.PrimaryScreen.Bounds.Width;
                        int    iHeight = Screen.PrimaryScreen.Bounds.Height;
                        Bitmap img     = new Bitmap(iWidth, iHeight);

                        Graphics gc = Graphics.FromImage(img);
                        gc.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
                        img.Save(pipeStream, ImageFormat.Jpeg);

                        pipeStream.WaitForPipeDrain();
                    }
                }
                catch { }
            }
        }
Exemplo n.º 2
0
        protected static void StartCamera(string[] args)
        {
            if (args.Length < 1)
            {
                return;
            }

            int    timeout  = 5000;
            string pipeName = args[0];

            if (args.Length >= 2)
            {
                int.TryParse(args[1], out timeout);
            }
            using (System.IO.Pipes.NamedPipeServerStream pipeStream = new System.IO.Pipes.NamedPipeServerStream(pipeName))
            {
                try
                {
                    pipeStream.WaitForConnection();
                    if (pipeStream.IsConnected)
                    {
                        Camera        camera = new Camera();
                        Task <Bitmap> task   = camera.CaptureImageAsync(timeout); //直接Wait这个task会产生死锁,因此异步开始、在后面循环判断执行结果。

                        while (task.Status != TaskStatus.RanToCompletion)
                        {
                            switch (task.Status)
                            {
                            case TaskStatus.Faulted: throw task.Exception;

                            case TaskStatus.Canceled: throw new TaskCanceledException();

                            default:
                            {
                                Thread.Sleep(1);
                                Application.DoEvents();
                            }
                            break;
                            }
                        }

                        Bitmap img = task.Result;
                        img.Save(pipeStream, ImageFormat.Jpeg);

                        pipeStream.WaitForPipeDrain();
                    }
                }
                catch { }
            }
        }