|
Programmatically Take Screenshot Using C#
To do this you will need to add references to System.Drawing and System.Windows.Forms. Make sure that you have following using statements:
using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms;
In your method add this code. It will take a screenshot of your primary screen and save it to a file.
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height); Graphics graphics = Graphics.FromImage(bitmap as Image); graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size); bitmap.Save(@"c:\temp\screenshot.bmp", ImageFormat.Bmp);
Leave a Reply
6563 views, 5 so far
today |
Get Updates By Email
Popular Post
Tag Cloud
Code Snippets
- Get Current Windows User In C#
- Get Width And Height Of Image In C#
- Get Windows Registry Size With WMI And C#
- Reverse Array Elements Using C#
- Convert Hexadecimal To Number In C#
- Get Free Disk Space Using T-SQL
- SQL Server 2008 – Get All Indexes In A Database
- Get Name Of Current Executing Assembly In C#
- Get CD Or DVD Drive Information Using WMI And C#
- Get Last Row From Table Using LINQ To SQL


April 2nd, 2009 at 1:58 am
Hello!
Is it possible to get a screenshot of a window that is not on the top, i.e. if it is partially hided by another window, without bringing it to the top?
Thanx
TJ
April 2nd, 2009 at 6:39 am
Hi TJ,
I have never tried this. But I believe it should be possible maybe using Win32 APIs.
July 10th, 2009 at 2:23 pm
Yes, you can use the win32 api to get a screen shot of any window.
If it’s minimized or hidden, the graphics device will likely be blanked out as the apps don’t generally paint then.
I posted a class to do it here:
http://www.machinegods.com/node/18
Here’s the method of interest without all the pinvoke declarations:
public static Bitmap Get(IntPtr hWnd)
{
WINDOWINFO winInfo = new WINDOWINFO();
bool ret = GetWindowInfo(hWnd, ref winInfo);
if (!ret)
{
return null;
}
int height = winInfo.rcWindow.Height;
int width = winInfo.rcWindow.Width;
if (height == 0 || width == 0) return null;
Graphics frmGraphics = Graphics.FromHwnd(hWnd);
IntPtr hDC = GetWindowDC(hWnd); //gets the entire window
//IntPtr hDC = frmGraphics.GetHdc(); — gets the client area, no menu bars, etc..
System.Drawing.Bitmap tmpBitmap = new System.Drawing.Bitmap(width, height, frmGraphics);
Graphics bmGraphics = Graphics.FromImage(tmpBitmap);
IntPtr bmHdc = bmGraphics.GetHdc();
BitBlt(bmHdc, 0, 0,width, height, hDC, 0, 0, TernaryRasterOperations.SRCCOPY);
bmGraphics.ReleaseHdc(bmHdc);
ReleaseDC(hWnd, hDC);
return tmpBitmap;
}
July 12th, 2009 at 9:58 am
Thank you Michael for the code sample.
April 20th, 2010 at 2:03 am
How would you only screen shot part of the screen ?
I would just like to take a screenshot from a portion of the screen.
Thanks !
May 19th, 2010 at 5:38 am
You would just specify a rectangle that wasn’t the full window size when calling bitblt.
so the 0,0,width,height in the line:
BitBlt(bmHdc, 0, 0,width, height, hDC, 0, 0, TernaryRasterOperations.SRCCOPY);
would change to whatever rectangle you needed it to be.
Of course, you’d also want to size the destination image to be the same size.
Here’s the bitblt reference.
http://msdn.microsoft.com/en-us/library/dd183370(VS.85).aspx
May 20th, 2010 at 11:50 am
Wow, Michael , really thanks so much for you’re help. I really appreciate you taking you’re time to explain this.
Cheers.