//****************************************************************************************************** // ScreenArea.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 10/01/2008 - J. Ritchie Carroll // Generated original version of source code. // 08/10/2009 - Josh L. Patterson // Edited Comments. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; using GSF.Drawing; namespace GSF.Windows.Forms { /// Returns screen area statistics and capture functionality for all connected screens. public static class ScreenArea { /// /// Gets the least "x" coordinate of all screens on the system /// /// The smallest visible "x" screen coordinate public static int LeftMostBound { get { int leftBound = int.MaxValue; // Return the left-most "x" screen coordinate foreach (Screen display in Screen.AllScreens) { if (leftBound > display.Bounds.X) leftBound = display.Bounds.X; } return leftBound; } } /// /// Gets the greatest "x" coordinate of all screens on the system /// /// The largest visible "x" screen coordinate public static int RightMostBound { get { int rightBound = int.MinValue; // Return the right-most "x" screen coordinate foreach (Screen display in Screen.AllScreens) { if (rightBound < display.Bounds.X + display.Bounds.Width) rightBound = display.Bounds.X + display.Bounds.Width; } return rightBound; } } /// /// Gets the least "y" coordinate of all screens on the system /// /// The smallest visible "y" screen coordinate public static int TopMostBound { get { int topBound = int.MaxValue; // Return the top-most "y" screen coordinate foreach (Screen display in Screen.AllScreens) { if (topBound > display.Bounds.Y) topBound = display.Bounds.Y; } return topBound; } } /// /// Gets the greatest "y" coordinate of all screens on the system /// /// The largest visible "y" screen coordinate public static int BottomMostBound { get { int bottomBound = int.MinValue; // Return the bottom-most "y" screen coordinate foreach (Screen display in Screen.AllScreens) { if (bottomBound < display.Bounds.Y + display.Bounds.Height) bottomBound = display.Bounds.Y + display.Bounds.Height; } return bottomBound; } } /// /// Gets the width of the screen with the highest resolution. /// /// The width of the screen with the highest resolution. public static int MaximumWidth { get { int maxWidth = int.MinValue; // In this case we just get the largest screen height foreach (Screen display in Screen.AllScreens) { if (maxWidth < display.Bounds.Width) maxWidth = display.Bounds.Width; } return maxWidth; } } /// /// Gets the width of the screen with the lowest resolution. /// /// The width of the screen with the lowest resolution. public static int MinimumWidth { get { int minWidth = int.MaxValue; // In this case we just get the smallest screen height foreach (Screen display in Screen.AllScreens) { if (minWidth > display.Bounds.Width) minWidth = display.Bounds.Width; } return minWidth; } } /// /// Gets the height of the screen with the highest resolution. /// /// The height of the screen with the highest resolution. public static int MaximumHeight { get { int maxHeight = int.MinValue; // In this case we just get the largest screen height foreach (Screen display in Screen.AllScreens) { if (maxHeight < display.Bounds.Height) maxHeight = display.Bounds.Height; } return maxHeight; } } /// /// Gets the height of the screen with the lowest resolution. /// /// The height of the screen with the lowest resolution. public static int MinimumHeight { get { int minHeight = int.MaxValue; // In this case we just get the smallest screen height foreach (Screen display in Screen.AllScreens) { if (minHeight > display.Bounds.Height) minHeight = display.Bounds.Height; } return minHeight; } } /// /// Gets the total width of all the screens relative to their arrangement. /// /// The total width of all the screens relative to their arrangement. public static int TotalWidth { get { return RightMostBound - LeftMostBound; } } /// /// Gets the total height of all the screens relative to their arrangement. /// /// The total height of all the screens relative to their arrangement. public static int TotalHeight { get { return BottomMostBound - TopMostBound; } } /// /// Performs screen capture over all monitors. /// /// Captured screen image over all monitors. /// /// Size captured will be for total width and height of all screens relative to their arrangement. /// An image square will be created large enough to cover all screens for the capture. /// public static Bitmap Capture() { return Capture(new Rectangle(LeftMostBound, TopMostBound, TotalWidth, TotalHeight)); } /// /// Performs screen capture over all monitors. /// /// Desired for captured . /// Captured screen image over all monitors. /// /// Size captured will be for total width and height of all screens relative to their arrangement. /// An image square will be created large enough to cover all screens for the capture. /// public static Bitmap Capture(ImageFormat imageFormat) { return Capture(new Rectangle(LeftMostBound, TopMostBound, TotalWidth, TotalHeight), imageFormat); } /// /// Performs screen capture for given . /// /// A object to capture. /// Captured screen image for given . public static Bitmap Capture(Screen captureScreen) { return Capture(captureScreen.Bounds); } /// /// Performs screen capture for given and . /// /// Desired to capture /// Desired for captured . /// Captured screen image for given . public static Bitmap Capture(Screen captureScreen, ImageFormat imageFormat) { return Capture(captureScreen.Bounds, imageFormat); } /// /// Performs a screen capture the given . /// /// Screen area to capture. /// Captured screen image for given area. public static Bitmap Capture(Rectangle captureArea) { return Capture(captureArea, ImageFormat.Bmp); } /// /// Performs a screen capture the given and . /// /// Screen area to capture. /// Desired for captured . /// Captured screen image for given area. public static Bitmap Capture(Rectangle captureArea, ImageFormat imageFormat) { // Create a blank image of the specified size. using (Bitmap screenCaptureImage = new Bitmap(captureArea.Width, captureArea.Height)) { using (Graphics screenCaptureGraphics = Graphics.FromImage(screenCaptureImage)) { // Copy the area of the screen to the blank image. screenCaptureGraphics.CopyFromScreen(captureArea.X, captureArea.Y, 0, 0, captureArea.Size); } // We'll return the captured screenshot in the specified image format. return screenCaptureImage.ConvertTo(imageFormat, true); } } } }