Skip to main content

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

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

Post a Comment

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

Popular posts from this blog

How to create Rest API(Web API) with ASP.NET Core 1.0

Recently Microsoft has released ASP.NET Core 1.0 RC2, I am getting lots of request from readers that about creating Web API. So I thought it will be a good idea to write a blog post about how to create Rest API(Web API) with ASP.NET Core 1.0.

So let’s get started via creating an ASP.NET Core 1.0 Web Application like following.



Once you click on ASP.NET Web Application, It will ask whether you need to create Web Application or Web API application. We are going to Web API so I am going to select Web API Application like following. Please note that in ASP.NET Core 1.0 there is no separate libraries or DLLs required for creating web APIs. This is just a project template.



Now once you click on OK It will create a Web API application with default values controller and program.cs.  As you know Program.cs is now starting point for the ASP.NET Core 1.0 application so It contains all the required configuration and startup items. Following is a code for that.
using System.IO; using Microsoft.Asp…

How to use migration with Entity Framework Core

Entity framework core is  the lightweight, extensible and cross-platform version of Entity Framework. Before some time, Microsoft has Released a new version of Entity Framework RC2. I have written a couple of blog post about Entity framework code first migration earlier for Entity framework 6.0. So there was a couple of request coming for writing a blog post about Entity Framework Core RC2 migration. So I thought it will be a good idea to give an overview how database migration works in Entity Framework Core RC2. This post will cover a basic scenario where we are going to create the database with existing ASP.NET Identity migration and then we are going to create a new model and have that migration applied in the database.

How to use Entity Framework Migrations: Let’s get started, To demonstrate entity framework core migrations, I am going to create a sample asp.net core web application like following.



Once we select asp.net core application it will appear the following dialog.



Now w…

Creating PDF with ASP.Net MVC and RazorPDF

Update: I have written a new blog post about better approach to create a PDF with asp.net mvc- You can find that following location.- A Better Solution to create PDF with Rotativa and ASP.NET MVC
In this post we are going to learn how we can easily create PDF from ASP.Net application with the help of Razor PDF NuGet package.

About Razor PDF:
This NuGet package is created by Al Nyveldt It internally uses ITextSharp an open source PDF convertor library. RazorPDF uses Razor View engine to create iTextXML which is in tern used to produce PDF file. You can get more information about that at below link.

https://www.nuget.org/packages/RazorPDF

Example(Creating PDF with ASP.Net MVC):
So what we are we waiting for ? Let’s create a simple example. To create example first thing we need to a create and ASP.Net MVC application.