Beispiel #1
0
 private static void TryCapture(CapturedObj lastFrame)
 {
     // do capture again only if last frame was >= 1/25 second old
     DateTime newTime = DateTime.Now;
     if (lastFrame.LastCaptured.AddMilliseconds(40) < newTime)
     {
         byte[] newFrame = ScreenShot.CaptureImage1024x768(new Point(0, 0), new Point(0, 0), Screen.PrimaryScreen.Bounds);
         lastFrame.LastCaptured = newTime;
         lastFrame.Image = newFrame;
     }
 }
Beispiel #2
0
        public static void SimpleListenerExample(string ipAddress, string port)
        {
            if (!HttpListener.IsSupported)
            {
                throw new Exception("该软件只支持Windows XP SP2以上,或者Server 2003以上。");
            }

            string[] prefixes = new string[] { "http://" + ipAddress + ":" + port + "/" };

            CapturedObj lastFrame = new CapturedObj()
            {
                Image = new byte[] { },
                LastCaptured = DateTime.Now,
            };

            // URI prefixes are required,
            // for example "http://contoso.com:8080/index/".
            if (prefixes == null || prefixes.Length == 0)
                throw new ArgumentException("prefixes");

            // Create a listener.
            using (HttpListener listener = new HttpListener())
            {
                // Add the prefixes.
                foreach (string s in prefixes)
                {
                    listener.Prefixes.Add(s);
                }

                listener.Start();
                HttpListenerContext context;
                HttpListenerRequest request;
                HttpListenerResponse response;
                while (true)    // keep serving image to clients until exit manually by the main thread.
                {
                    // Note: The GetContext method blocks while waiting for a request.
                    context = listener.GetContext();
                    request = context.Request;

                    if (request.RawUrl.ToLower().Contains("image"))   // get frame request
                    {
                        try
                        {
                            response = context.Response;
                            response.ContentType = "image/jpeg";

                            TryCapture(lastFrame);

                            response.ContentLength64 = lastFrame.Image.Length;
                            Stream output = response.OutputStream;
                            output.Write(lastFrame.Image, 0, lastFrame.Image.Length);
                            output.Close();
                            response.Close();
                        }
                        catch (Exception)
                        {
                            // sometimes it failed because of client aborted, so ignore output fail.
                        }
                    }
                    else
                    {
                        // don't care any other request, such as "/favicon.ico" fired by browser, etc.
                    }
                }
            }
        }