def _cli_screenshot(self, path): bmp = Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height) graphics = Graphics.FromImage(bmp) try: graphics.CopyFromScreen(0, 0, 0, 0, bmp.Size) finally: graphics.Dispose() bmp.Save(path, Imaging.ImageFormat.Jpeg)
def pick(snip=False): snipper = PickSnipTool(PickSnipTool.take_screenshot(), snip=snip) while True: result = snipper.ShowDialog() if result in [DialogResult.OK, DialogResult.Cancel]: break if snipper.mouse_down_seconds == 1: Mouse.Instance.Click(snipper.mouse_down) time.sleep(0.5) snipper.BackgroundImage = PickSnipTool.take_screenshot() if result == DialogResult.OK and snip: if snipper.snip_rectangle.Width and snipper.snip_rectangle.Height: img = Bitmap( snipper.snip_rectangle.Width, snipper.snip_rectangle.Height, ) gr = Graphics.FromImage(img) gr.DrawImage( snipper.BackgroundImage, Rectangle(0, 0, img.Width, img.Height), snipper.snip_rectangle, GraphicsUnit.Pixel, ) fp = MemoryStream() img.Save(fp, ImageFormat.Png) return { 'bytes': bytes(bytearray(fp.ToArray())), } return {} elif result == DialogResult.OK: if (snipper.mouse_down_seconds and snipper.mouse_down_seconds <= snipper.click_timeout): Mouse.Instance.Click(snipper.mouse_down) time.sleep(0.5) el = AutomationElement.FromPoint(snipper.mouse_up) result = { prop.ProgrammaticName.split('.', 1)[-1]: el.GetCurrentPropertyValue(prop) for prop in el.GetSupportedProperties() } result.update({ 'NameProperty': el.GetCurrentPropertyValue(el.NameProperty), 'ControlTypeProperty': el.GetCurrentPropertyValue( el.ControlTypeProperty, ).ProgrammaticName.split('.', 1)[-1], 'AutomationIdProperty': el.GetCurrentPropertyValue(el.AutomationIdProperty, ), }) return result else: return {}
def take_screenshot(as_bytes=False): bounds = Screen.PrimaryScreen.Bounds screenshot = Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppPArgb) graphics = Graphics.FromImage(screenshot) graphics.CopyFromScreen(0, 0, 0, 0, screenshot.Size) if as_bytes: fp = MemoryStream() screenshot.Save(fp, ImageFormat.Png) return bytes(bytearray(fp.ToArray())) else: return screenshot