How to capture web page screenshot in asp.net using c#

In this tutorial you will learn how to capture webpage screenshot in asp.net using c#. You may got a requirement to capture the screenshot when user submits the form , etc. Most of the people purchase third party dll files and use them in their project to capture the screenshot of webpage. It is quite easy in asp.net and there is no need to purchase any dll. Let's have a look over how to do so.

How to capture web page screenshot in asp.net using c#

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Web.Services;
using System.Text;
using System.Windows.Forms;//must be included for capturing screenshot

public partial class capture_webpage_screenshot : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
        graphics.CopyFromScreen(25, 25, 25, 25, bitmap.Size);
        bitmap.Save(@"c:\screenshot\myscreenshot.bmp", ImageFormat.Bmp);
       
    }
}

The code is ready. Just you have to do the following steps.

  1. Right click the project name
  2. Add reference -> .net tab and then choose system.windows.forms namespace then press ok button.
  3. In .aspx code behind file you have to import the System.Windows.Forms namespace and that’s it.
By customizing parameters used in CopyFromScreen() function, you can figure out how to capture the small portion of the screen, if required. Create a folder and give folder path in c# code just like above i have given. You can customize the code as well and give the dynamic name of bitmap file every time it is generated.


Happy Coding!!!

4 comments:

  • sani
     

    HI i am having a problem @3rd line
    "graphics.CopyFromScreen(25, 25, 25, 25, bitmap.Size);"

    the error is
    "The handle is invalid"
    how can i handle this
    Tx in advance

  • sani
     

    i am having problem @
    graphics.CopyFromScreen(25, 25, 25, 25, bitmap.Size);

    the error is "The handle is invalid"

    can u pls tell wat i need to do
    tx in advance

  • Adeel Fakhar
     

    Hi sani, Please add these three namspaces in your page like this

    using System.Drawing.Imaging;
    using System.IO;
    using System.Drawing;


    Its working fine here, i checked my code and found these namespaces missing here so please add them as well.

  • Adeel Fakhar
     

    Hi sani, i have checked the code, it is working fine here. As far as your problem is concerned, please add these three namespaces in your csharp code

    using System.Drawing.Imaging;
    using System.IO;
    using System.Drawing;


    Have fun!!!