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);
7 Responses to Programmatically Take Screenshot Using C#
Leave a Reply Cancel reply
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Free Icons And Images With Visual Studio 2008
- Capture XML In WCF Service
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML


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
Hi TJ,
I have never tried this. But I believe it should be possible maybe using Win32 APIs.
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;
}
Thank you Michael for the code sample.
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 !
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
Wow, Michael , really thanks so much for you’re help. I really appreciate you taking you’re time to explain this.
Cheers.