Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture the Screen into a Bitmap

I want to capture the screen in my code to get an image - like using the 'print screen' button on the keyboard .

Does anyone have an idea how to do this? I have no starting point.

like image 794
Omar Abid Avatar asked Dec 12 '08 14:12

Omar Abid


People also ask

How do I capture screen image?

Press the Power and Volume down buttons at the same time. If that doesn't work, press and hold the Power button for a few seconds. Then tap Screenshot.

How do I capture part of a screen?

Press “Windows + Shift + S”. Your screen will appear grayed out and your mouse cursor will change. Click and drag on your screen to select the part of your screen you want to capture. A screenshot of the screen region you selected will be copied to your clipboard.


2 Answers

If using the .NET 2.0 (or later) framework you can use the CopyFromScreen() method detailed here:

http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html

//Create a new bitmap. var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,                                Screen.PrimaryScreen.Bounds.Height,                                PixelFormat.Format32bppArgb);  // Create a graphics object from the bitmap. var gfxScreenshot = Graphics.FromImage(bmpScreenshot);  // Take the screenshot from the upper left corner to the right bottom corner. gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,                             Screen.PrimaryScreen.Bounds.Y,                             0,                             0,                             Screen.PrimaryScreen.Bounds.Size,                             CopyPixelOperation.SourceCopy);  // Save the screenshot to the specified path that the user has chosen. bmpScreenshot.Save("Screenshot.png", ImageFormat.Png); 
like image 142
Gary Willoughby Avatar answered Oct 16 '22 00:10

Gary Willoughby


I had two problems with the accepted answer.

  1. It doesn't capture all screens in a multi-monitor setup.
  2. The width and height returned by the Screen class are incorrect when display scaling is used and your application is not declared dpiAware.

Here's my updated solution using the Screen.AllScreens static property and calling EnumDisplaySettings using p/invoke to get the real screen resolution.

using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms;  class Program {     const int ENUM_CURRENT_SETTINGS = -1;      static void Main()     {         foreach (Screen screen in Screen.AllScreens)         {             DEVMODE dm = new DEVMODE();             dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));             EnumDisplaySettings(screen.DeviceName, ENUM_CURRENT_SETTINGS, ref dm);              using (Bitmap bmp = new Bitmap(dm.dmPelsWidth, dm.dmPelsHeight))             using (Graphics g = Graphics.FromImage(bmp))             {                 g.CopyFromScreen(dm.dmPositionX, dm.dmPositionY, 0, 0, bmp.Size);                 bmp.Save(screen.DeviceName.Split('\\').Last() + ".png");             }         }     }      [DllImport("user32.dll")]     public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);      [StructLayout(LayoutKind.Sequential)]     public struct DEVMODE     {         private const int CCHDEVICENAME = 0x20;         private const int CCHFORMNAME = 0x20;         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]         public string dmDeviceName;         public short dmSpecVersion;         public short dmDriverVersion;         public short dmSize;         public short dmDriverExtra;         public int dmFields;         public int dmPositionX;         public int dmPositionY;         public ScreenOrientation dmDisplayOrientation;         public int dmDisplayFixedOutput;         public short dmColor;         public short dmDuplex;         public short dmYResolution;         public short dmTTOption;         public short dmCollate;         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]         public string dmFormName;         public short dmLogPixels;         public int dmBitsPerPel;         public int dmPelsWidth;         public int dmPelsHeight;         public int dmDisplayFlags;         public int dmDisplayFrequency;         public int dmICMMethod;         public int dmICMIntent;         public int dmMediaType;         public int dmDitherType;         public int dmReserved1;         public int dmReserved2;         public int dmPanningWidth;         public int dmPanningHeight;     } } 

References:

https://stackoverflow.com/a/36864741/987968 http://pinvoke.net/default.aspx/user32/EnumDisplaySettings.html?diff=y

like image 40
colton7909 Avatar answered Oct 16 '22 00:10

colton7909