Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

asp.net

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.

like image 898
Srikar Doddi Avatar asked May 26 '10 19:05

Srikar Doddi


People also ask

How do I capture content from a website?

On Windows, the key combination Alt + PrtSc (Print Screen) captures the currently active window. Like on Mac, the screenshot will be copied into the clipboard, so you can insert it into other software by pressing Ctrl + V . The non-shortcut way on Windows is called Snipping Tool.


1 Answers

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);
    }
}
like image 116
Icemanind Avatar answered Sep 22 '22 11:09

Icemanind