capture contents of a window and save as image

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

capture contents of a window and save as image

Ricardo Birmann
Hi,

I've been looking for a way to create screenshots of my application using VW7.4. It is a simulation program and I would very much like to save an image of the screen on every simulation cycle (for presentation purposes and stuff like that)

ok, here are my questions:

1- I've noticed the method Screen >> contentsOfArea:. From it I can easily get an Image object, but how to give this method the proper argument, i.e., how can I get the rectangle corresponding to my window's currently displayed area?... is there an easier way to do this?

2- Ok, let's say I have the image I want as an Image object... can this be saved into a file (.PNG, .JPG, ... anything will do)?

Thanks in advance for any comments and ideas....

Best Regards,

Ricardo


Reply | Threaded
Open this post in threaded view
|

Re: capture contents of a window and save as image

Joachim Geidel
Hi Ricardo,

> I've been looking for a way to create screenshots of my application
> using VW7.4. It is a simulation program and I would very much like to
> save an image of the screen on every simulation cycle (for presentation
> purposes and stuff like that)

There are several ways to do this:

| image window |
window := Window currentWindow.
image := window completeContentsOfArea: window bounds.
ArborGIFReaderWriter
        putImage: image ad2CollapsedTo8BitsOrLess
        onFileNamed: 'window.gif'.
(JPEG.Writer yCbCr) write: image to: 'window.jpg'.
(JPEG.Writer grayscale) write: image to: 'windowGray.jpg'.

If you want to include the window borders, try something like

Screen default completeContentsOfArea:
        ((window bounds translatedBy: window globalOrigin)
                insetOriginBy: -2@-25 cornerBy: -2@-2)

The ArborGIFReaderWriter comes with the VisualWorks help system, if I'm
not mistaken. Unfortunately, it can only store Images with 8 bits per
pixel or less. The method Image>>ad2CollapsedTo8BitsOrLess does the
conversion. It is implemented in ADvance2, which is one of the optional
Developer Tools. I don't suggest including ADvance in your application,
but the method shows how this can be done.

JPEG.Writer is part of the JPEG bundle in the Cincom Public Repository.

More file types are available in Jun (comes as goodie with VisualWorks).
Jun supports BMP, JPEG, GIF, PICT, and VisualWorks BOSS format. There is
a subset of Jun called Takenoko which has a more liberal licence
("Modified BSD Licence"), if this is of concern. You can get it from
http://www.sra.co.jp/people/aoki/Takenoko/

With Jun / Takenoko, you can store an image like this:

aStream := JunJpegImageStream on: aFilename writeStream.
[Cursor write showWhile: [aStream nextPutImage: anImage]]
        ensure: [aStream close]]]

If you need other output formats, you could export GIF or JPEG files and
then use e.g. IrfanView or another image processing utility.

If you are using Windows, you could also try to capture the bitmaps
using the freeware GrabCaptureScreen (see
http://www.grabcapturescreen.com). It has a "PrtSc grabbing" option
which grabs all bitmaps from the clipboard automatically. The feature
described in the help file under "Program options" > "Grabbing options".
In your application, you just have to place all the bitmaps into the
clipboard, e.g. like this:

| pixmap window |
window := Window currentWindow.
pixmap := Pixmap extent: window extent.
[window component displayOn: pixmap graphicsContext.
pixmap toClipboard]
                valueNowOrOnUnwindDo: [pixmap close]

GrabCaptureScreen will store them automatically to the directory and
with the filenames and types which you configure in its option dialog.

There may be other tools in the public domain capable of doing this, and
probably also tools which can be controlled with command line options,
such that you could place a bitmap in the clipboard, then store it by
launching the tool from VisualWorks.

HTH,
Joachim

Reply | Threaded
Open this post in threaded view
|

Re: capture contents of a window and save as image

Andre Schnoor
In reply to this post by Ricardo Birmann
Ricardo,
here's a simple solution. It renders GIF files from applications, the
screen and arbitrary visual components.
HTH,
Andre


ApplicationModel>>screenShot: aFilename
    "Save top application window to GIF file"
    self builder window topComponent
        screenShot: aFilename

Window>>screenShot: aFilename
    "Save the receiver as a GIF file"
    | gc pixmap t box |
    box := self bounds expandedBy: 2@2.
    pixmap := Pixmap extent: box extent on: Screen default.
    gc := ScreenGraphicsContext on: pixmap.
    gc paint: ColorValue black.
    gc displayRectangle: box.
    t := gc translation.
    gc translateBy: 1@1.
    self displayOn: gc.
    pixmap screenShot: aFilename.
    gc translation: t

VisualComponent>>screenShot: aFilename
    "Save the receiver as a GIF file"
    | pixmap |
    pixmap := Pixmap extent: self bounds extent on: Screen default.
    self displayOn: (ScreenGraphicsContext on: pixmap).
    pixmap screenShot: aFilename.

VisualPart>>screenShot: aFilename
    "Save the receiver as a GIF file"
    | pixmap gc box t |
    box := self bounds expandedBy: 2@2.
    pixmap := Pixmap extent: box extent on: Screen default.
    gc := ScreenGraphicsContext on: pixmap.
    "frame:"
    gc paint: ColorValue black.
    gc displayRectangle: box.
    t := gc translation.
    gc translateBy: 1@1.
    "be sure to add the window background color:"
    gc paint: self topComponent backgroundColor.
    gc displayRectangle: self bounds.
    self displayOn: gc.
    pixmap screenShot: aFilename.
    gc translation: t

DisplaySurface>>screenShot: aFilename
    "Save the receiver as a GIF file"
    self asImage screenShot: aFilename.

Image>>screenShot: aFilename
    "Save the receiver as a GIF file. This is where all screenshots
     get finally forwared to."
    ArborGIFReaderWriter
        putImage: (self copy convertToPalette: (MappedPalette
                withColors: (self uniqueColorValues: 255) asArray))
        onFileNamed: aFilename.

WidgetWrapper>>screenShot: aFilename
    "Be sure to delegate to the sub builder component here"
    self widget screenShot: aFilename.

Image>>uniqueColorValues
    | cols |
    cols := Set new.
    self pixelsDo: [:x :y | cols add: (self valueAtPoint: x@y)].
    ^cols

Image>>uniqueColorValues: anInteger
    "Answer an Array of the receiver's colors limited to the arguments
size."
    | cols recent minGap minIndex i d protect |
    protect := ColorValue white.
    cols := self uniqueColorValues asSortedCollection asOrderedCollection.
    [ cols size > anInteger ] whileTrue:[
        recent := minGap := minIndex := nil.
        i := 0.      
        cols do:[ :c |
            i := i + 1.
            recent notNil ifTrue:[
                d := recent distanceFrom: c.
                (minGap isNil or:[ d < minGap ])
                    ifTrue:[
                        c = protect ifFalse:[
                            minGap := d. minIndex := i ]]].
            recent := c.
        ].
        cols removeIndex: minIndex.
    ].
    ^cols


--

Ricardo Birmann wrote:

> Hi,
>
> I've been looking for a way to create screenshots of my application
> using VW7.4. It is a simulation program and I would very much like to
> save an image of the screen on every simulation cycle (for
> presentation purposes and stuff like that)
>
> ok, here are my questions:
>
> 1- I've noticed the method Screen >> contentsOfArea:. From it I can
> easily get an Image object, but how to give this method the proper
> argument, i.e., how can I get the rectangle corresponding to my
> window's currently displayed area?... is there an easier way to do this?
>
> 2- Ok, let's say I have the image I want as an Image object... can
> this be saved into a file (.PNG, .JPG, ... anything will do)?
>
> Thanks in advance for any comments and ideas....
>
> Best Regards,
>
> Ricardo
>
>

Reply | Threaded
Open this post in threaded view
|

RE: capture contents of a window and save as image

Steven Kelly
In reply to this post by Ricardo Birmann

You can also export to PNG, which is probably the best choice for screenshots these days (>256 colours, non-lossy compression):

http://www.refactory.com/Software/PNG.zip

For usage, see StandardSystemController>>saveAsPNG. For sensible speed you'll also want to load ZLibInterface, which makes the compression use the VM's compression routines rather than Smalltalk code:

http://www.refactory.com/Software/ZLibInterface.zip

Both require Refactory-Namespace, which is in a package in the Tools-Refactoring Browser bundle in the Tools-IDE bundle in 7.4.1.

Thanks to John Brant for creating these and making them available!

HTH,

Steve

-----Original Message-----
From: Ricardo Birmann [mailto:[hidden email]]
Sent:
18 July 2006 07:30
To: vwnc
Subject: capture contents of a window and save as image

 

Hi,

I've been looking for a way to create screenshots of my application using VW7.4. It is a simulation program and I would very much like to save an image of the screen on every simulation cycle (for presentation purposes and stuff like that)

ok, here are my questions:

1- I've noticed the method Screen >> contentsOfArea:. From it I can easily get an Image object, but how to give this method the proper argument, i.e., how can I get the rectangle corresponding to my window's currently displayed area?... is there an easier way to do this?

2- Ok, let's say I have the image I want as an Image object... can this be saved into a file (.PNG, .JPG, ... anything will do)?

Thanks in advance for any comments and ideas....

Best Regards,

Ricardo

Reply | Threaded
Open this post in threaded view
|

[7.4][OS X]Trouble with Image Editor

Carl Gundel
In reply to this post by Ricardo Birmann
I wanted to try changing the button background for Pollock toolbars so
that it would look more like a real Aqua toolbar button.  At first I
just replaced the image with an all-white one.  That was an improvement,
but I then decided to try capture the toolbar graphic so I could use
that.  On my Powerbook I opened the VW Image Editor and tried to use the
image capture feature.  The graphic that resulted looked sort of like a
negative image, but it also just looked corrupted.  I tried this with
both the standard Mac VM and the X11 VM with the same result.  I also
tried rebooting the machine with the same result.

-Carl Gundel, author of Liberty BASIC
http://www.libertybasic.com


Reply | Threaded
Open this post in threaded view
|

RE: [7.4][OS X]Trouble with Image Editor

Steven Kelly
> -----Original Message-----
> From: Carl Gundel [mailto:[hidden email]]
> Sent: 18 July 2006 16:06
> To: vwnc
> Subject: [7.4][OS X]Trouble with Image Editor
>
> I wanted to try changing the button background for Pollock toolbars so
> that it would look more like a real Aqua toolbar button.  At first I
> just replaced the image with an all-white one.  That was an
improvement,
> but I then decided to try capture the toolbar graphic so I could use
> that.  On my Powerbook I opened the VW Image Editor and tried to use
the
> image capture feature.  The graphic that resulted looked sort of like
a
> negative image, but it also just looked corrupted.  I tried this with
> both the standard Mac VM and the X11 VM with the same result.  I also
> tried rebooting the machine with the same result.

The same problem exists in Windows (XP, 32-bit colors). It's been there
for a while.

Steve

Reply | Threaded
Open this post in threaded view
|

Re: [7.4][OS X]Trouble with Image Editor

Carl Gundel
>> image capture feature.  The graphic that resulted looked sort of like
>a
>> negative image, but it also just looked corrupted.  I tried this with
>> both the standard Mac VM and the X11 VM with the same result.  I also
>> tried rebooting the machine with the same result.
>
>The same problem exists in Windows (XP, 32-bit colors). It's been there
>for a while.

It works fine for me on XP (just tried it again to make sure).  :-/

-Carl Gundel, author of Liberty BASIC
http://www.libertybasic.com



Reply | Threaded
Open this post in threaded view
|

Re: [7.4][OS X]Trouble with Image Editor

Samuel S. Shuster <sames@interaccess.com>
Carl,

>It works fine for me on XP (just tried it again to make sure).  :-/

In MacOSX there is a tool called Grab (in utilities if I remember). Grab the
area of the screen/window, then save it as a BMP image or something (Jpeg or
Png). Then use :

ImageReader imageFromFile: '\VisualWorks\myImage.bmp
        toClass: AClassIUseToManageStuff
        selector: #myImage

                                And So It Goes
                                     Sames
______________________________________________________________________

Samuel S. Shuster [|]
VisualWorks Engineering, GUI Project
Smalltalk Enables Success -- What Are YOU Using?

Reply | Threaded
Open this post in threaded view
|

Re: [7.4][OS X]Trouble with Image Editor

Travis Griggs

On Jul 18, 2006, at 10:16, Samuel S. Shuster wrote:

Carl,

It works fine for me on XP (just tried it again to make sure).  :-/

In MacOSX there is a tool called Grab (in utilities if I remember). Grab the
area of the screen/window, then save it as a BMP image or something (Jpeg or
Png). Then use :

Hehe, that's how I used to do it too. Then I discovered that Preview can 'Grab' too (under 'File' menu). And then you can view it right there. And have lots of them in tabs. So on and so forth. I never use the standalone 'Grab' utility anymore.

--
Travis Griggs
Objologist
My Other Machine runs OSX. But then... so does this one.



DISCLAIMER: This email is bound by the terms and conditions described at http://www.key.net/disclaimer.htm

Reply | Threaded
Open this post in threaded view
|

Re: capture contents of a window and save as image

Ricardo Birmann
In reply to this post by Steven Kelly
Thanks everyone for replying,

I've managed to do what I wanted using Joachim's suggestions. I've created a simple workspace script to generate some .gif images according to some events in my application.

Later on I convert the gif's to png's and use mencoder to generate the video (i'm running linux, so these things are easily automated with a 5 lines long bash script)

Thanks again for your help

Ricardo


On 7/18/06, Steven Kelly <[hidden email]> wrote:

You can also export to PNG, which is probably the best choice for screenshots these days (>256 colours, non-lossy compression):

<a href="http://www.refactory.com/Software/PNG.zip" title="http://www.refactory.com/Software/PNG.zip" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://www.refactory.com/Software/PNG.zip

For usage, see StandardSystemController>>saveAsPNG. For sensible speed you'll also want to load ZLibInterface, which makes the compression use the VM's compression routines rather than Smalltalk code:

<a href="http://www.refactory.com/Software/ZLibInterface.zip" title="http://www.refactory.com/Software/ZLibInterface.zip" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://www.refactory.com/Software/ZLibInterface.zip

Both require Refactory-Namespace, which is in a package in the Tools-Refactoring Browser bundle in the Tools-IDE bundle in 7.4.1.

Thanks to John Brant for creating these and making them available!

HTH,

Steve

-----Original Message-----
From: Ricardo Birmann [mailto:[hidden email]]
Sent:
18 July 2006 07:30
To: vwnc
Subject: capture contents of a window and save as image

 

Hi,

I've been looking for a way to create screenshots of my application using VW7.4. It is a simulation program and I would very much like to save an image of the screen on every simulation cycle (for presentation purposes and stuff like that)

ok, here are my questions:

1- I've noticed the method Screen >> contentsOfArea:. From it I can easily get an Image object, but how to give this method the proper argument, i.e., how can I get the rectangle corresponding to my window's currently displayed area?... is there an easier way to do this?

2- Ok, let's say I have the image I want as an Image object... can this be saved into a file (.PNG, .JPG, ... anything will do)?

Thanks in advance for any comments and ideas....

Best Regards,

Ricardo


Reply | Threaded
Open this post in threaded view
|

Re: [7.4][OS X]Trouble with Image Editor

Roland Wagener
In reply to this post by Carl Gundel
Hi Alltogether



I tested the 'Image fromUser' stuff yesterday on my 'old' G4 Powerbook with MacOS 10.3.9, and was happy to receive a full color image. I actualy used the ImageEditor, although Travis CoolImage is much better ;-)

The Screen is set to Millions of Colors.

But the biggest drawback using VisualWorks as a Grab-Tool is that when your want to grab details of an active MacOS scroll bar from an other application window, this other window gets inactivated when VisualWorks is active. So no way to get it directly into VW.

Travis' suggestion is correct to use a MacOS system utility and then import the image file into VW, or (not tested for a long time) use the Clipboard and something similar to:

  Pixmap fromClipboard

and the secret messages you need to convert the Pixmap into an Image (sorry, can't find them right now).



Greetings



Roland Wagener