In this tutorial we will learn how to create a screen capture application in REALbasic. We will discover three different methods to grab the screen form Mac OS, Windows or Linux desktop and load it to Canvas Control. Watch it, learn it, enjoy it!
Before we will learn to the universal way (cross platform) to grab the screen and load it into Canvas Control in REALbasic we will look at screencapture, a Terminal command used to invoke the screen capture feature in Mac OS X. Using Shell Class we will execute a command that will capture the screen and save it on our system desktop. Add a PushButton to Window1 and in it Sub Action () enter:
1 2 3 | Dim s As New Shell s.Execute ("screencapture -S -x ~/Desktop/screenshot.jpg") |
The screencapture utility is not very well documented to date. A list of options follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | -S In window capture mode, capture the screen instead of the window. -W Start interaction in window selection mode. -c Force screen capture to go to the clipboard. -i Capture screen interactively, by selection or window. The control key will cause the screen shot to go to the clipboard. The space key will toggle between mouse selection and window selection modes. The escape key will cancel the interactive screen shot. -m Only capture the main monitor, undefined if -i is set. -s Only allow mouse selection mode. -w Only allow window selection mode. -x Do not play sounds. -C Capture the cursor as well as the screen. Only allowed in non-interactive modes. -t Image format to create, default is png. file where to save the screen capture |
We can also try to grab the screen by simulating PrtSc key click. Add to Window1 a PushButton Control and Canvas Control. Now in PushButton Sub Action () enter:
1 2 3 4 5 6 7 | Dim c As New Clipboard Declare Sub keybd_event Lib "User32" ( keyCode as Integer, scanCode as Integer, flags as Integer, extraData as Integer ) keybd_event( &h2C, 0, 0, 0 ) //simulate pressing the PrtSc key if c.PictureAvailable then Canvas1.Backdrop = c.Picture //load picture to Canvas Control |
This method will work on Linux, Mac and Windows. Add to Window1 a PushButton Control and Canvas Control. Now in PushButton Sub Action () enter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Dim r As RGBSurface Dim screenshot As Picture screenshot = New Picture (Canvas1.Width, Canvas1.Height, 32) r = screenshot.RGBSurface for i as integer = 0 to width for j as integer = 0 to height r.pixel(i, j) = system.pixel(i, j) next next Canvas1.Graphics.Drawpicture screenshot, 0, 0 |
If you are creating a screen capture application in REALbasic you probably like to know how to open a image, how to fit graphic to canvas, how to crop the picture or display scaled image. In my earlier tutorial “Creating ImageViewer in REALbasic” you can find all answers on these questions.
can only make the capture of the program? windows
I don’t use MS Windows but I think that there is a option to get a screen shot form program window. We must simulate pressing the PrtSc key + Control Key (keybd_event(17,29,0,0)).
On mac we can grab a screen shot form program window using command:
screencapture -W ~/Desktop/screenshot.jpg
The Windows command keybd_event(17,29,0,0) does not work.
Is there a way to take a Windows screen shot of the program window without placing and grabbing the information from the clipboard?
The last method Screencapture on Linux using REALbasic works on Windows too but it’s not perfect. REALbasic don’t offer this kind of option so the best way is calling some Windows declarations.