Srikar Doddi
Srikar Doddi

Reputation: 15609

How to capture a screen shot in .NET from a webapplication?

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.

Upvotes: 8

Views: 4510

Answers (4)

Lars Truijens
Lars Truijens

Reputation: 43645

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.

Upvotes: 4

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391724

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?

Upvotes: 1

Icemanind
Icemanind

Reputation: 48736

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);
    }
}

Upvotes: 6

Related Questions