How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?

We can get the element screenshot by cropping entire page screenshot as below: driver.get(“http://www.google.com”); WebElement ele = driver.findElement(By.id(“hplogo”)); // Get entire page screenshot File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); BufferedImage fullImg = ImageIO.read(screenshot); // Get the location of element on the page Point point = ele.getLocation(); // Get width and height of the element int eleWidth = … Read more

How to convert a UIView to an image

An extension on UIView should do the trick. extension UIView { // Using a function since `var image` might conflict with an existing variable // (like on `UIImageView`) func asImage() -> UIImage { let renderer = UIGraphicsImageRenderer(bounds: bounds) return renderer.image { rendererContext in layer.render(in: rendererContext.cgContext) } } } Apple discourages using UIGraphicsBeginImageContext starting iOS 10 … Read more

Capture the Screen into a Bitmap

If using the .NET 2.0 (or later) framework you can use the CopyFromScreen() method detailed here: http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html //Create a new bitmap. var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb); // Create a graphics object from the bitmap. var gfxScreenshot = Graphics.FromImage(bmpScreenshot); // Take the screenshot from the upper left corner to the right bottom corner. gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, … Read more

iOS Detection of Screenshot?

As of iOS 7 the other answers are no longer true. Apple has made it so touchesCancelled:withEvent: is no longer called when the user takes a screenshot. This would effectively break Snapchat entirely, so a couple betas in a new solution was added. Now, the solution is as simple as using NSNotificationCenter to add an … Read more

Get a screenshot of a specific application

The PrintWindow win32 api will capture a window bitmap even if the window is covered by other windows or if it is off screen: [DllImport(“user32.dll”)] public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [DllImport(“user32.dll”)] public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags); public static Bitmap PrintWindow(IntPtr hwnd) { RECT rc; GetWindowRect(hwnd, out … Read more

Take a screenshot via a Python script on Linux

This works without having to use scrot or ImageMagick. import gtk.gdk w = gtk.gdk.get_default_root_window() sz = w.get_size() print “The size of the window is %d x %d” % sz pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1]) pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1]) if (pb != None): pb.save(“screenshot.png”,”png”) print “Screenshot saved to screenshot.png.” else: print “Unable to get the screenshot.” Borrowed from http://ubuntuforums.org/showpost.php?p=2681009&postcount=5

How Do I Take a Screen Shot of a UIView?

iOS 7 has a new method that allows you to draw a view hierarchy into the current graphics context. This can be used to get an UIImage very fast. I implemented a category method on UIView to get the view as an UIImage: – (UIImage *)pb_takeSnapshot { UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale); [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES]; // … Read more

Is there a way to take a screenshot using Java and save it to some sort of image?

Believe it or not, you can actually use java.awt.Robot to “create an image containing pixels read from the screen.” You can then write that image to a file on disk. I just tried it, and the whole thing ends up like: Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); BufferedImage capture = new Robot().createScreenCapture(screenRect); ImageIO.write(capture, “bmp”, new File(args[0])); … Read more

How to take a screenshot programmatically on iOS

Considering a check for retina display use the following code snippet: #import <QuartzCore/QuartzCore.h> if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale); } else { UIGraphicsBeginImageContext(self.window.bounds.size); } [self.window.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData *imageData = UIImagePNGRepresentation(image); if (imageData) { [imageData writeToFile:@”screenshot.png” atomically:YES]; } else { NSLog(@”error while taking screenshot”); }