Screen Capture
From IronPython Cookbook
Screen Capture
The below code explains how to capture screen using a simple IronPython code. The Code is a simple windows forms application. The heart of the code is in cmdcaptureclicked function. Here Screen class is used to get the screen width and height. Using Bitmap class we grab the image and buffered into a temporary memory.
When run, it minimizes itself and displays a screenshot in a PictureBox:
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Drawing import Bitmap, Graphics, Point, Size
from System.Windows.Forms import (
Application, Button, Form, FormWindowState,
PictureBox, Screen
)
frm = Form(Text="Capture Screen")
frm.ClientSize = Size(290, 288)
pb = PictureBox()
pb.Location = Point(0, 0)
pb.Size = Size(100, 50)
frm.Controls.Add(pb)
def cmdcaptureclicked(sender, args):
frm.WindowState = FormWindowState.Minimized
bmp = Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
g = Graphics.FromImage(bmp)
g.CopyFromScreen(0, 0, 0, 0, bmp.Size)
g.Dispose()
pb.Image = bmp
pb.Size = bmp.Size
cmdcapture = Button()
cmdcapture.Text = "Capture Screen"
cmdcapture.Location = Point(169, 249)
cmdcapture.Size = Size(110, 30)
cmdcapture.Click += cmdcaptureclicked
frm.Controls.Add(cmdcapture)
def main():
Application.EnableVisualStyles()
Application.Run(frm)
if __name__ == "__main__":
main()
--Sunil Pottumuttu 06:19, 14 August 2007 (CDT)
Back to Contents.


