resize an image to the icon button size
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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 ();
Copy link to clipboard
Copied
Thank you for your answer,
but it works only on "image", not on "iconbutton".
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
can you write to me an example please?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
cropping an image is not always the right way, sometimes a better way is to scale your images to button...

