is there an easy way (preferably without having to import libraries) to take a screenshot of an ASP.NET web page (better yet an aspx control) in c# and saving it as an image? Many thanks in advance! Sample code or a link to a tutorial would be greatly appreciated...
-
4+1: just because I hate it when people downvote without leaving comments. – Dathan Mar 02 '11 at 19:42
-
2do you mean client or server side? – BrokenGlass Mar 02 '11 at 19:44
-
1@Dathan - especially since this question doesn't seem too bad. +1 from me too – Adam Rackis Mar 02 '11 at 19:48
-
1"without having to import libraries" what does that even mean? I'll never understand the phobia some people have of using tools to make their lives easier, especially when in this case Watin will do this in 1 line of code. – Brook Mar 02 '11 at 20:17
-
@Brook, +1 for Watin. It's also possible with [awesomium](http://www.khrona.com/products/awesomium) – Kalman Speier Mar 02 '11 at 21:49
4 Answers
Here is a simple screenshot maker, wrote a few years ago. I am not sure what you like to achieve, but this one takes a screenshot of the whole screen. Hope this helps.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
public class ScreenshotManager
{
private Image screenshot;
public Image Screenshot
{
get
{
if (screenshot == null)
MakeScreenshot();
return screenshot;
}
}
public MemoryStream ScreenshotToMemoryStream()
{
MemoryStream ms = new MemoryStream();
Screenshot.Save(ms, ImageFormat.Jpeg);
ms.Position = 0;
return ms;
}
public byte[] ScreenshotToByteArray()
{
return ScreenshotToMemoryStream().ToArray();
}
public void MakeScreenshot()
{
screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
var graphics = Graphics.FromImage(screenshot);
graphics.CopyFromScreen(0, 0, Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
}
}

- 1,937
- 13
- 18
-
Hey. Thanks for the code. I m trying to use it, but am getting a few errors - graphics, bitmap, screen and save are not recognized. Do I need to import something? Thanks! – MiziaQ Mar 02 '11 at 20:09
-
-
also add please how to use this code for asp.net. Something like this, for example, please: ScreenshotManager sm = new ScreenshotManager(); System.Drawing.Image im = sm.Screenshot; System.IO.MemoryStream ms = new System.IO.MemoryStream(); im.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); – apros Mar 02 '11 at 20:36
-
@apros, actually you can call it from ASP.NET, even a controller action of MVC, but it doesn't make sense in this case. Because I think he wants to take a screenshot of the rendered web page (ASPX). So it is just a basic sample class what he can use in a WinForms or WPF application for example to take a screenshot from a web page. – Kalman Speier Mar 02 '11 at 20:46
-
yes, you are right, but your code + mentioned above code will give a screenshot of a rendered aspx page if you add it to Button_Click event handler in code behind for some aspx page. Anyway thanks you for sharing! – apros Mar 02 '11 at 21:02
Not 100% sure if this is exactly what you are looking for, but this tutorial should at least give you the ground work ability to capture a screen shot and save it. This more appears to be capping the entire screen, rather than just the aspx page, but at least it should be a start.
http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html
http://weblogs.asp.net/jalpeshpvadgama/archive/2008/01/28/how-to-take-screenshot-in-c.aspx
The biggest piece of that seems to be
using System.Drawing.Imaging;
Should be able to hash things out from there, I believe

- 3,326
- 3
- 22
- 34
You can programmatically create a web browser control and take a screenshot of its client area -contrary to popular belief the web browser does not have to be visible for this - just make sure the thread you use the WebBrowser
control on runs in apartment state ApartmentState.STA
. This approach will work both on the server and client side.
On a high level what you have to do to create the bitmap:
Instantiate the WebBrowser control and set the desired width/height
Browse to the URI of your choice
Wait for the
DocumentCompleted
eventUse the WebBrowser
DrawToBitmap()
method to extract the image

- 158,293
- 28
- 286
- 335