Reputation: 137
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...
Upvotes: 1
Views: 3232
Reputation: 160852
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
event
Use the WebBrowser DrawToBitmap()
method to extract the image
Upvotes: 0
Reputation: 1937
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);
}
}
Upvotes: 1
Reputation: 7446
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.
Upvotes: 2
Reputation: 3361
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
Upvotes: 0