/* ScreenCapturer C# source Lee Felarca http://www.zeropointnine.com/blog v0.9 - 2009-04-18 Runs in the background, and feeds screencapture image data to external processes via standard I/O Source code licensed under a Creative Commons Attribution 3.0 License. http://creativecommons.org/licenses/by/3.0/ Some Rights Reserved. Improvements made to this class are encouraged. */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Runtime.InteropServices; using System.Timers; namespace ScreenCapturer { public partial class ScreenCapturer : Form { public const string sendImage_COMMAND = "GET"; public const string EXIT_COMMAND = "EXIT"; public const uint STARTCODE = 9999999; public const uint ENDCODE = 6666666; private byte[] _ba; public ScreenCapturer() { InitializeComponent(); go(); } private void go() { string s; do { s = System.Console.In.ReadLine(); } while (s == null || s.Length == 0); // does not lock CPU (despite appearances) string[] a = s.Split(' '); string command = a[0]; switch (command) { case sendImage_COMMAND: if (a.Length != 5) break; int w = Int32.Parse(a[1]); int h = Int32.Parse(a[2]); int offx = Int32.Parse(a[3]); int offy = Int32.Parse(a[4]); sendImage(w, h, offx, offy); break; case EXIT_COMMAND: Application.Exit(); break; } go(); } private void sendImage(int w, int h, int offx, int offy) { _ba = getScreenCapture(w, h, offx, offy); Stream console = Console.OpenStandardOutput(); BinaryWriter binaryWriter = new BinaryWriter(console); // compose 'header' and send in one chunk MemoryStream msh = new MemoryStream(); BinaryWriter bwh = new BinaryWriter(msh); bwh.Write(STARTCODE); // start code bwh.Write((int)w); // image width bwh.Write((int)h); // image height byte[] bah = msh.ToArray(); binaryWriter.Write(bah); // dump image info, all at once binaryWriter.Write(_ba); // send termination code binaryWriter.Write(ENDCODE); } private byte[] getScreenCapture(int w, int h, int offx, int offy) { int startTime = DateTime.Now.Millisecond; Bitmap bitmap = new Bitmap(w, h); Graphics g = Graphics.FromImage(bitmap); g.CopyFromScreen(new Point(offx, offy), Point.Empty, new Size(w, h)); // * how expensive is this? byte[] ba = bitmapToBytes(bitmap); // benchmark info int constructionTime = DateTime.Now.Millisecond - startTime; System.Diagnostics.Trace.WriteLine("Time required to compose data: " + constructionTime); return ba; } private unsafe byte[] bitmapToBytes(Bitmap bmp) { BitmapData bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); // number of bytes in the bitmap int byteCount = bData.Stride * bmp.Height; byte[] bmpBytes = new byte[byteCount]; // Copy the locked bytes from memory Marshal.Copy(bData.Scan0, bmpBytes, 0, byteCount); // don't forget to unlock the bitmap bmp.UnlockBits(bData); return bmpBytes; } } }