Subscribe Bookmark RSS Feed

Changing a Picture Size

msharp

Super User

Joined:

Jul 28, 2015

I have tens of pictures I want my users to be able to re-size easily.  I built a small function that does the trick but I feel there has to be a better way.  Below is an example of what I'm doing.  Since the pictures are always changing, I only know the reference to the picture box.  One of the issues with this strategy is that by re-sizing the picture several times it degrades the quality.

I was wondering if anyone has used the << Inval, or << Inval Size; I feel one of these two methods would be slicker.

Also, does anyone know the difference between << Get Image; and << Get Picture?  The results from the Get Picture are slightly smaller and the BLOB's are completely different - get Image being larger.

Names Default to Here(1);

img = new image("$SAMPLE_IMAGES/tile.jpg");

imgSize = 300;

img << Set Size({imgSize,imgSize});

New Window("Test", vlistbox(Slider Box(100,1000,imgSize),

       Button Box("Set Size",

              tempImage = pb << Get Image;

              tempImage << Set Size({imgSize,imgSize});

              pb << Set Image(tempImage);

       ),

       pb = picturebox(img)));

4 REPLIES
ms

Super User

Joined:

Jun 23, 2011

If the reference to the original image is unknown in the context the code is run, one solution for the degradation issue may be an invisible copy of the original picture box.

An example (not any slicker though):

nw = New Window("Test",

    V List Box(

        Slider Box(100, 1000, imgSize),

        Button Box("Set Size",

            origImage = pb1 << Get Image;

            origImage << Set Size({imgSize, imgSize});

            pb << Set Image(origImage);

        ),

        pb = Picture Box(img),

        pb1 = Picture Box(img);

        pb1<< visibility(collapsed)

    )

);

Justin_Chilton

Joined:

Aug 27, 2015

Simarlily, you could just keep a reference to the original image in order to avoid the collapsed picture box.

Names Default To Here( 1 );

originalImg = New Image( "$SAMPLE_IMAGES/tile.jpg" );

img = New Image( "$SAMPLE_IMAGES/tile.jpg" );

imgSize = 300;

img << Set Size( {imgSize, imgSize} );

New Window( "Test",

V List Box(

Slider Box( 100, 1000, imgSize ),

Button Box( "Set Size",

img = New Image( originalImg );

img << Set Size( {imgSize, imgSize} );

pb << Set Image( img );

),

pb = Picture Box( img )

)

);

Justin
msharp

Super User

Joined:

Jul 28, 2015

MS This is an interesting solution to keep the image resolution clear.

Justin, as noted above, the images in the picture box are changing.  Keeping track of the images instead of picture boxes would be more work then I want to put into this.

Craige_Hales

Staff

Joined:

Mar 21, 2013

(Turn off the Translate check box at the top of the page if this text looks messed up)

<<getPicture can be sent to any display box; it causes the box and its children to be drawn into a bitmap.

<<getImage can be sent to either a PictBox or a GraphBox.  For the GraphBox, it does about the same thing as <<getPicture.  For the PictBox it clones the existing image without redrawing it.

You probably want to use <<getImage with a PictBox and <<getPicture for GraphBox and other boxes.

Using getPicture with a PictBox will re-encode the pixels if you save it; you might avoid the re-encoding with getImage and saving to the same format.  Re-encoding can degrade the image quality, especially with JPG.

update:  I'm also fond of the frame box (Graph Box) <<savePicture command:

g[framebox(1)]<<savepicture(dir||char(seq)||".png","png");


Craige