/// <summary> /// Provides a server object for a screen stream. /// </summary> /// <param name="resolutions">Stream Resolution.</param> /// <param name="fps">FPS.</param> /// <param name="isDisplayCursor">Whether to display the cursor in screenshots.</param> /// <returns>The object of the StreamingServer class.</returns> public static StreamingServer GetInstance(Resolution.Resolutions resolutions, Fps fps, bool isDisplayCursor) { lock (s_syncRoot) { s_serverInstance ??= new StreamingServer(resolutions, fps, isDisplayCursor); } return(s_serverInstance); }
/// <summary> /// Provides enumeration of screenshots. /// </summary> /// <param name="requiredResolution">Required screenshot resolution.</param> /// <param name="isDisplayCursor">Whether to display the cursor in screenshots.</param> /// <returns>Enumeration of screenshots.</returns> public static IEnumerable <Image> TakeSeriesOfScreenshots(Resolution.Resolutions requiredResolution, bool isDisplayCursor) { var screenSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); var requiredSize = Resolution.GetResolutionSize(requiredResolution); var rawImage = new Bitmap(screenSize.Width, screenSize.Height); var rawGraphics = Graphics.FromImage(rawImage); bool isNeedToScale = screenSize != requiredSize; var image = rawImage; var graphics = rawGraphics; if (isNeedToScale) { image = new Bitmap(requiredSize.Width, requiredSize.Height); graphics = Graphics.FromImage(image); } var source = new Rectangle(0, 0, screenSize.Width, screenSize.Height); var destination = new Rectangle(0, 0, requiredSize.Width, requiredSize.Height); while (true) { rawGraphics.CopyFromScreen(0, 0, 0, 0, screenSize); if (isDisplayCursor) { AddCursorToScreenshot(rawGraphics, source); } if (isNeedToScale) { graphics.DrawImage(rawImage, destination, source, GraphicsUnit.Pixel); } yield return(image); } }
/// <summary> /// Initializes the fields and properties of the class for the screen stream. /// </summary> private StreamingServer(Resolution.Resolutions imageResolution, Fps fps, bool isDisplayCursor) : this(Screenshot.Screenshot.TakeSeriesOfScreenshots(imageResolution, isDisplayCursor), fps) { }