Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to find the ImageSize or at least dimensions of any control object irrespective of it being an image or an any other control such as Buttons,etc.

This is my code for button:

button = Button["ew", Null, ImageSize -> {100, 50}]

ImageDimensions provides size of images only and not of control objects. So, initially, I rasterized my control object using Rasterize. And then displayed the image dimensions. However the image dimensions is not exact value.

rastButton = Rasterize[button, RasterSize -> 100]  
bt = Button["ew", Null, {ImageSize -> {100, 50}}]  
rastbt = Rasterize[bt, RasterSize -> 100]  
ImageSize[rastbt]  
ImageDimensions[rastbt]  
(*  Out[272] = {100, 52} *)

bt = Button["ew", Null, {ImageSize -> {100, 1}}]
rastbt = Rasterize[bt, RasterSize -> 100]
ImageSize[rastbt]
ImageDimensions[rastbt]
(* Out[272] = {100, 17} *)

I also believe Rasterize provides error values when I call Rasterize[button][[2, 2]]. Is this a bug? Is there any workaround to estimate the size of control objects?

Ultimately, the purpose of estimating the size is to place the control object very accurately in a grid layout within specified co ordinates.

share|improve this question

2 Answers

If you use the Option ImageSize for Rasterize then it is correct. Notice that 15 seems to be the minimum height for a button, such that the Rasterize result agrees.

   button=Button["ew",Null,ImageSize->{100,50}]
    rastButton=Rasterize[button,ImageSize->{100,50}]
    bt=Button["ew",Null,{ImageSize->{100,50}}]
    rastbt=Rasterize[bt,ImageSize->{100,50}]

    ImageDimensions[rastbt]
 (*
==> {100,50}
  *)
    bt=Button["ew",Null,{ImageSize->{100,15}}]
    rastbt=Rasterize[bt,ImageSize->{100,15}]
    ImageSize[rastbt]
    ImageDimensions[rastbt]
    (*
==> {100,15}
*)

Using ImportString and ExportString

ImageDimensions[
   ImportString[ExportString[#, "TIFF"], "TIFF"]] &@button

(* gives {100,52} *)

Appearance is the clue here:

buts = Button["xx", Null, Appearance -> #] & /@ {"DialogBox", 
   "Palette", "FramedPalette", "Frameless", 
   "Pressed", {"DialogBox", "Pressed"}}

Mathematica graphics

Mathematica graphics

share|improve this answer
I used your idea to solve my problem as follows bt = Button[sd, Null, ImageSize -> {10, 20}]; Rasterize[bt][[2, 2]] ImageDimensions[Rasterize[bt, ImageSize -> {10, 20}]] Rasterize[bt][[2, 2]] It gives me result as (*{10, 28},{10, 20},{10, 28}*). I want to find exact button size. The idea you have given is Rasterize the button size and give result, but i want to extract exact size. – niren Jan 17 at 8:17
So that i could place it with correct co ordinate and correct size irrespective of its original size. – niren Jan 17 at 8:27

It seems form the documentation that the right function for that is AbsoluteCurrentValue[]. Indeed the description: ?AbsoluteCurrentValue coincides pretty close with what is looked for. However,

pl = Plot[ Sin[t], {t, 0, 2 \[Pi]}, ImageSize -> 100];
AbsoluteCurrentValue[pl, ImageSize]

returns {350, 350} and also

pl = Plot[ Sin[t], {t, 0, 2 \[Pi]}, ImageSize -> 200];
AbsoluteCurrentValue[pl, ImageSize]

returns {350, 350}. ??

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.