Files
IT-Nexus/agent-cs/CaptureModeRunner.cs

63 lines
2.3 KiB
C#
Raw Normal View History

using System.Drawing;
using System.Drawing.Imaging;
using System.IO.Pipes;
namespace ITNexusAgent;
// Läuft als User-Prozess (via schtasks), nicht als SYSTEM-Service
// Captured den Desktop und schickt JPEG-Frames via Named Pipe an den Service
public static class CaptureModeRunner
{
private static readonly ImageCodecInfo JpegCodec =
ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == ImageFormat.Jpeg.Guid);
public static void Run(string pipeName)
{
if (string.IsNullOrEmpty(pipeName)) return;
try
{
using var pipe = new NamedPipeClientStream(".", pipeName, PipeDirection.Out);
pipe.Connect(5000); // 5s timeout
var encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 60L);
// Alle Screens zusammenführen (Multi-Monitor-Support)
var allScreens = System.Windows.Forms.Screen.AllScreens;
int left = allScreens.Min(s => s.Bounds.X);
int top = allScreens.Min(s => s.Bounds.Y);
int right = allScreens.Max(s => s.Bounds.X + s.Bounds.Width);
int bottom = allScreens.Max(s => s.Bounds.Y + s.Bounds.Height);
int totalW = right - left;
int totalH = bottom - top;
while (pipe.IsConnected)
{
try
{
using var bmp = new Bitmap(totalW, totalH, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (var g = Graphics.FromImage(bmp))
g.CopyFromScreen(left, top, 0, 0, new Size(totalW, totalH));
byte[] jpeg;
using (var ms = new MemoryStream())
{
bmp.Save(ms, JpegCodec, encParams);
jpeg = ms.ToArray();
}
// Länge (4 Bytes LE) + JPEG-Daten
pipe.Write(BitConverter.GetBytes(jpeg.Length));
pipe.Write(jpeg);
pipe.Flush();
}
catch { break; }
Thread.Sleep(150); // ~6-7 fps
}
}
catch { /* Pipe nicht verfügbar oder Timeout → Exit */ }
}
}