1

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

MiziaQ
  • 127
  • 1
  • 6
  • 12

4 Answers4

2

One really kludgie solution: Write a WinForms app and include a Browser control. Navigate to the web app page you're trying to capture, and then use the programmatic screen capture approach described here.

Community
  • 1
  • 1
Dathan
  • 7,266
  • 3
  • 27
  • 46
1

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);
    }
}
Kalman Speier
  • 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
  • Sure, I have added the using clause to the original post. – Kalman Speier Mar 02 '11 at 20:22
  • 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
0

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

guildsbounty
  • 3,326
  • 3
  • 22
  • 34
0

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:

  1. Instantiate the WebBrowser control and set the desired width/height

  2. Browse to the URI of your choice

  3. Wait for the DocumentCompleted event

  4. Use the WebBrowser DrawToBitmap() method to extract the image

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335