Skip to main content
yon1313
Known Participant
April 29, 2016
Question

resize an image to the icon button size

  • April 29, 2016
  • 5 replies
  • 1750 views

hi,

how can i resize an image to the icon button size?

var w = new Window ("dialog");

b=w.add ("iconbutton", undefined, "~/Desktop/abc.png");

b.size=[50,50]

w.show();

This topic has been closed for replies.

5 replies

yon1313
yon1313Author
Known Participant
May 1, 2016

cropping an image is not always the right way, sometimes a better way is to scale your images to button...

Smallpath2013
Inspiring
May 1, 2016

Resizing the image is not good when script has many images since it cost a lot of time.A better way is to crop the image.

yon1313
yon1313Author
Known Participant
April 30, 2016

can you write to me an example please?

yon1313
yon1313Author
Known Participant
April 29, 2016

Thank you for your answer,

but it works only on "image", not on "iconbutton".

UQg
Brainiac
April 30, 2016

I think that the only way to scale the image of the icon button is to overwrite the onDraw method for the button (as in Marc Autret snippet), but then this would also overwrite the default (built-in) graphic behaviour of the iconbutton (mouseover, mousedown, etc), which means that you would need to write more code to handle this as well (add corresponding event listeners to the iconbutton, prepare specific images for the mouseover/mousedown states, etc). In other words, you'll loose all the benefits of the built-in class and have to rewrite everything. Worth it ?

Xavier.

Alex White
Brainiac
April 29, 2016

There is an example from Marc Autret inside ScriptUI for dummies by Peter Kahrel .

It shows how to resize the image, but I think it would probably work for iconbutton too:

Image.prototype.onDraw = function()

{ // written by Marc Autret

// "this" is the container; "this.image" is the graphic

if( !this.image ) return;

var WH = this.size,

wh = this.image.size,

k = Math.min(WH[0]/wh[0], WH[1]/wh[1]),

xy;

// Resize proportionally:

wh = [k*wh[0],k*wh[1]];

// Center:

xy = [ (WH[0]-wh[0])/2, (WH[1]-wh[1])/2 ];

this.graphics.drawImage(this.image,xy[0],xy[1],wh[0],wh[1]);

WH = wh = xy = null;

}

var w = new Window ("dialog", "Bouquet");

var flowers = w.add ("image", undefined, File ("/d/scriptui/bouquet.jpg"));

flowers.size = [50,50];

w.show ();