// Start serving up frames private void DoIt(Capture cam, TcpServer serv, StreamWriter sw, ImageCodecInfo myImageCodecInfo, EncoderParameters myEncoderParameters) { MemoryStream m = new MemoryStream(20000); Bitmap image = null; IntPtr ip = IntPtr.Zero; do { // Wait til a client connects before we start the graph ConnectionReady.WaitOne(); cam.Start(); // While not shutting down, and still at least one client while ((!bShutDown) && (serv.Connections > 0)) { try { // capture image ip = cam.GetBitMap(); image = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, ip); image.RotateFlip(RotateFlipType.RotateNoneFlipY); // save it to jpeg using quality options m.Position = 10; image.Save(m, myImageCodecInfo, myEncoderParameters); // Send the length as a fixed length string m.Position = 0; m.Write(Encoding.ASCII.GetBytes( (m.Length - 10).ToString("d8") + "\r\n"), 0, 10); // send the jpeg image serv.SendToAll(m); // Empty the stream m.SetLength(0); // remove the image from memory image.Dispose(); image = null; } catch(Exception ex) { try { sw.WriteLine(DateTime.Now.ToString()); sw.WriteLine(ex); } catch {} } finally { if (ip != IntPtr.Zero) { Marshal.FreeCoTaskMem(ip); ip = IntPtr.Zero; } } } // Clients have all disconnected. Pause, then sleep and wait for more cam.Pause(); sw.WriteLine("Dropped frames: " + cam.m_Dropped.ToString()); } while ( !bShutDown ); }
// Start serving up frames private void DoIt(WebCamRecorder.Capture cam, TcpServer serv, StreamWriter sw, ImageCodecInfo myImageCodecInfo, EncoderParameters myEncoderParameters) { MemoryStream m = new MemoryStream(20000); Bitmap image = null; IntPtr ip = IntPtr.Zero; msgListBox.Items.Add("Ready to serve!"); do { // Wait til a client connects before we start the graph ConnectionReady.WaitOne(); cam.Start(); msgListBox.Items.Add("requested"); // While not shutting down, and still at least one client while ((!bShutDown) && (serv.Connections > 0)) { try { // capture image ip = cam.GetBitMap(); image = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, ip); image.RotateFlip(RotateFlipType.RotateNoneFlipY); Bitmap bitmapOverlay = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bitmapOverlay); g.Clear(System.Drawing.Color.Transparent); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // Prepare to put the specified string on the image string text = DateTime.Now.ToString(); SizeF d = g.MeasureString(text, fontOverlay); float sLeft = 10; float sTop = 10; g.FillRectangle(System.Drawing.Brushes.Black, 7, 7, 250, 30); g.DrawString(text, fontOverlay, System.Drawing.Brushes.White, sLeft, sTop, System.Drawing.StringFormat.GenericTypographic); g.Dispose(); g = Graphics.FromImage(image); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // draw the overlay bitmap over the video's bitmap g.DrawImage(bitmapOverlay, 0, 0, bitmapOverlay.Width, bitmapOverlay.Height); // dispose of the various objects g.Dispose(); // save it to jpeg using quality options m.Position = 10; image.Save(m, myImageCodecInfo, myEncoderParameters); // Send the length as a fixed length string m.Position = 0; m.Write(Encoding.ASCII.GetBytes((m.Length - 10).ToString("d8") + "\r\n"), 0, 10); // send the jpeg image serv.SendToAll(m); // Empty the stream m.SetLength(0); // remove the image from memory image.Dispose(); image = null; } catch (Exception ex) { try { sw.WriteLine(DateTime.Now.ToString()); sw.WriteLine(ex); } catch { } } finally { if (ip != IntPtr.Zero) { Marshal.FreeCoTaskMem(ip); ip = IntPtr.Zero; } } } // Clients have all disconnected. Pause, then sleep and wait for more cam.Pause(); sw.WriteLine("Dropped frames: " + cam.m_Dropped.ToString()); } while (!bShutDown); }