blob: 6ade0cd0e9a97b92198fea5870fa7a7e21f02852 [file] [log] [blame] [raw]
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Windows.Forms;
namespace RemoteView.PageHandlers
{
class ScreenPageHandler : AbstractPageHandler
{
private volatile byte[][] caches = new byte[Screen.AllScreens.Length][];
private volatile int lastScreenRequested = 0;
private Screen[] screens = Screen.AllScreens;
private System.Timers.Timer timer;
/// <summary>
/// C'tor init cache for all screens
/// </summary>
public ScreenPageHandler()
{
for (int i = 0; i < caches.Length; i++)
{
caches[i] = serializeScreenImage(i);
}
timer = new System.Timers.Timer(1000);
timer.Elapsed += reloadCache;
timer.Enabled = true;
}
/// <summary>
/// reload image for last requested screen
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void reloadCache(object sender, System.Timers.ElapsedEventArgs e)
{
int toReload = lastScreenRequested;
#if DEBUG
Stopwatch perfCounter = new Stopwatch();
perfCounter.Start();
#endif
caches[toReload] = serializeScreenImage(toReload);
#if DEBUG
perfCounter.Stop();
Console.WriteLine("Time elapsed: {0}", perfCounter.Elapsed);
#endif
}
/// <summary>
/// Get a copy of the requested device screen image from cache
/// </summary>
/// <param name="response"></param>
/// <param name="uri"></param>
/// <returns></returns>
public override byte[] handleRequest(HttpListenerResponse response, String[] uri)
{
int requested = getRequestedScreenDevice(uri, screens);
lastScreenRequested = requested;
response.Headers.Set("Content-Type", "image/png");
return caches[requested];
}
/// <summary>
/// Serizalize a screen
/// </summary>
/// <param name="requested">screen to serialize</param>
/// <returns></returns>
private byte[] serializeScreenImage(int requested)
{
Screen screen = screens[requested];
using (MemoryStream ms = new MemoryStream())
{
Bitmap bmp = new Bitmap(screen.Bounds.Width, screen.Bounds.Height);
Graphics.FromImage(bmp).CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, screen.Bounds.Size);
bmp.Save(ms, ImageFormat.Png);
return ms.GetBuffer();
}
}
}
}