To create a screenshot in c# we need to use drawing api of the .net framework
First you have import System.Drawing.Imaging name space with following code...
using System.Drawing.Imaging;
here is the code in C# for screenshot.
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);
First you have import System.Drawing.Imaging name space with following code...
using System.Drawing.Imaging;
here is the code in C# for screenshot.
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);
Your code will leak handles. You should really dispose the Graphics-object and the Bitmap at the end. Here's an alternative version:
ReplyDeleteRectangle bounds = Screen.GetBounds(Point.Empty);
using(Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using(Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
bitmap.Save("test.jpg", ImageFormat.Jpeg);
}
Leak handles? This is managed code, why would you think that?
ReplyDeleteThe "Leak Handles" version is funny. Like the guy is a paranoid c++ freak from 10 years back. If that code was required to avoid leaks, I would slit my wrists right now and get the pain of programming with memory management of my chest right now.
ReplyDeleteI'm not paranoid and the last time I used C++ was when I was in college. I have over 5 years of solid experience in C# and .NET. It's not because you're writing in managed code that you can't leak resources. I'm not talking about leaking memory, which is handled by the garbage collector (but can still occur), but about leaking Win32-handles. Why do you think the IDisposable-interface and the using-construct exist? So you could release a handle (Graphics-object, file handle, ...) deterministically.
ReplyDeleteI suggest you don't slit your wrists and read up on it. Here's a recent discussion about the IDisposable-interface: http://stackoverflow.com/questions/254969/dealing-with-net-idisposable-objects
You sir, just got 'told'
ReplyDeleteHello!
ReplyDeleteIs 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
I would also like to point out that:
ReplyDeleteusing(...)
{
}
should be used for IDisposable types, otherwise they are not cleared by GC.
The using statement is the correct answer. The Garbage collector may take minutes, hours, days depending on you application to come by and close and dispose the connection. If this code would be called in a loop it could cause some memory leak issues as well as having poor performance. Managed code does not mean you can just write any old junk and the environment will clean up your mess. It is always better to write it correctly the first time and avoid issues later.
ReplyDeleteWhy is
ReplyDeleteusing(Graphics g = Graphics.FromImage(bitmap))
{
...
}
better than
Graphics g = Graphics.FromImage(bitmap);
...
g.Dispose();
I'd really be interested in a profound answer.
@Anonymous - There is not specific difference. In using object will be dispose automatically and method you suggested will explicitly call dispose method. So there will not be much difference.
ReplyDeleteThere is a difference: If your code in "..." throws an exception and this exception is caught in one of the calling methods your 'g' will not be disposed immediately.
ReplyDeleteAwesome tips on how to take screenshot in c#. Extremely detailed and quite good recommendations. Appreciate it
ReplyDeleteThe using pattern is a lot better than having to manually call Dispose because Dispose is guaranteed to be called when you leave the scope.
ReplyDeleteConsider the following example:
Graphics G = Graphigs.FromImage(bitmap)
...
some error condition occurs
throw new SomeException();
...
g.Dispose();
The graphics object doesn't get disposed unless you wrap the code in a try-finally block which is a lot less elegant than just wrapping the code in a using block.
how can i take screenshots and save it in picturebox
ReplyDeleteReally useful...Thanks.
ReplyDeleteHere is a detailed and structured one for those who like to copy/paste:
ReplyDeletehttp://www.dotnetobject.com/Thread-take-screenshot-in-windows-form-c