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.
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With