tags:

views:

123

answers:

4

In Java we can do it as follows:

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;

...

public void captureScreen(String fileName) throws Exception {

   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   Rectangle screenRectangle = new Rectangle(screenSize);
   Robot robot = new Robot();
   BufferedImage image = robot.createScreenCapture(screenRectangle);
   ImageIO.write(image, "png", new File(fileName));

}

...

How do we do this in .NET from a webapplication? Capturing the client's screen and sending it to the server all from within the application.

+1  A: 

http://www.codeproject.com/KB/cs/TeboScreen.aspx

Paja
That looks remarkably like a web application... or not...
Lasse V. Karlsen
Actually the requirement that the code must be for web app only was added after my answer... so thanks for vote down.
Paja
+5  A: 

The .NET graphics object has a method called CopyFromScreen() that will capture a rectangular area of the screen and copy it into a bitmap. The best way to do it is similar to the following:

public void CaptureImage(Point SourcePoint, Point DestinationPoint, Rectangle Selection, string FilePath)
{
    using (Bitmap bitmap = new Bitmap(Selection.Width, Selection.Height)) {
        using (Graphics g = Graphics.FromImage(bitmap)) {
            g.CopyFromScreen(SourcePoint,DestinationPoint, Selection.Size);
        }
        bitmap.Save(FilePath, ImageFormat.Bmp);
    }
}
icemanind
True, but now we know this has to be done from a webapplication this code alone is not the answer
Lars Truijens
Yes. If he is trying to capture the client's desktop screen, it can't be done from ASP.NET. Maybe an ActiveX, but it would only work on IE Windows users.
icemanind
How about silverlight? Otherwise you could add 'No' as an answer to this question with an explanation why.
Lars Truijens
should work on all browsers/os
CodeToGlory
With Silverlight 3.0 and up, there is an object caleld WriteableBitmap that will let you capture just the client area of the browser. You cannot capture the entire screen..just the area of the screen that the silverlight plugin occupies. You can get an example from here: http://stackoverflow.com/questions/2475140/how-to-capture-part-of-the-screen-in-silverlight
icemanind
Specifically it will draw the Silverlight application onto the bitmap.
Lasse V. Karlsen
How is Java AWT able to create these bitmaps through Applet but ASP.NET does not have a way to do it?
CodeToGlory
Well first of all I think its because it doesn't do what you think. The Java Robot object, I'm pretty sure, just captures the client area...Not the user's entire desktop screen. Secondly, you can't compare Java to ASP.NET. They are 2 different beasts. Java is an applet that is loaded from a webpage and is run on the client's machine. ASP.NET does not run on a client's machine. It runs strictly off a web server. Java applets are comparable more to Adobe Flash or Microsoft Silverlight.
icemanind
+4  A: 

No, there is no way this can be done using html or javascript alone. They simply do not have the methods to do it. A possible reason would be that it will be a security risk like John Saunders points out. Webapplications could capture anything happening on the users screen without them knowing about it.

Server side code like you have shown does not work, since it is run on the server side. Sliverlight or a ActiveX Form might work, but no option since you would like it to work on all browsers on all platforms.

edit

icemanind lets us known it is possible using Silverlight, but you can't capture the whole screen. Probably security reasons.

Lars Truijens
A: 

This is not possible with a basic web application, nor with Silverlight.

I also highly suspect that the Robot class in Java doesn't let you take a screenshot when running as a browser applet, otherwise that would be one of the biggest security holes ever found in Java, if a basic web application with a pixel-sized java applet could stream a live video of my desktop over the internet back to the server.

Let's take one step back, and ask this: What are you trying to accomplish? Why do you want to take the screenshot?

Lasse V. Karlsen
users are complaining about an issue that the life of me could not be replicated in QA or in any environment. It does not mean it is nothappening so we would like to see it firsthand
CodeToGlory
@CodeToGlory: Maybe it is best to ask a new question on how to capture what the user is doing for debugging purposes. Screencapture programs or desktop sharing applications come to mind.
Lars Truijens