How to take ScreenShot in C#

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);

16 comments:

  1. Your code will leak handles. You should really dispose the Graphics-object and the Bitmap at the end. Here's an alternative version:

    Rectangle 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);
    }

    ReplyDelete
  2. Leak handles? This is managed code, why would you think that?

    ReplyDelete
  3. The "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.

    ReplyDelete
  4. I'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.

    I 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

    ReplyDelete
  5. 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

    ReplyDelete
  6. I would also like to point out that:

    using(...)
    {
    }

    should be used for IDisposable types, otherwise they are not cleared by GC.

    ReplyDelete
  7. 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.

    ReplyDelete
  8. Why is
    using(Graphics g = Graphics.FromImage(bitmap))
    {
    ...
    }

    better than

    Graphics g = Graphics.FromImage(bitmap);
    ...
    g.Dispose();

    I'd really be interested in a profound answer.

    ReplyDelete
  9. @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.

    ReplyDelete
  10. There 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.

    ReplyDelete
  11. Awesome tips on how to take screenshot in c#. Extremely detailed and quite good recommendations. Appreciate it

    ReplyDelete
  12. The using pattern is a lot better than having to manually call Dispose because Dispose is guaranteed to be called when you leave the scope. 

    Consider 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.

    ReplyDelete
  13. how can i take screenshots and save it in picturebox

    ReplyDelete
  14. Really useful...Thanks.

    ReplyDelete
  15. Here is a detailed and structured one for those who like to copy/paste:

    http://www.dotnetobject.com/Thread-take-screenshot-in-windows-form-c

    ReplyDelete

Your feedback are very important to me. Please provide your feedback via putting comments.