5
$\begingroup$

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.

$\endgroup$

3 Answers 3

2
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – niren
    Commented Jan 17, 2013 at 8:17
  • $\begingroup$ So that i could place it with correct co ordinate and correct size irrespective of its original size. $\endgroup$
    – niren
    Commented Jan 17, 2013 at 8:27
1
$\begingroup$

Just my $0.02, though I don't think you'll like what I have to say. There used to be a function called Developer'BoundingBox which was succeeded by options to Rasterize. You do not actually have to rasterize an object to gets its size. Rasterize[expr,"BoundingBox"] is enough. You can also supply option "RasterSize" which will return the size of the final image, but that depends more on ImageSize, ImageResolution and other parameters than on the actual component itself.

Here's the deal breaker however... Bounding box depends on font sizes and rendering. So If your object has any of its parts measured in ems, like Grid with ItemSize, you will not have an accurate BoundingBox:

Works:

Works without ItemSize

Broken:

Broken with ItemSize

Anyone who can explain this will be my personal hero, as I need this to work properly VERY MUCH.

Thank you!

$\endgroup$
15
  • $\begingroup$ It would be better to post actual code followed by screenshots of the output. As it is seen from your "broken" example Rasterize[expr,"BoundingBox"] determines the bounding box correctly. What happens is that inside of Overlay the document width seems to be counted by Scaled ItemSize smaller than just inside of the Cell. $\endgroup$ Commented Oct 30, 2013 at 3:41
  • $\begingroup$ You're right about posting code in a copy-able way, my apologies, I rushed. You're touching on the core issue here: the Grid seems to be re-created in a Cell before it is rasterized. The problem is not just with use of Scaled, but line wrapping for long strings and other events which can occur during this process that cause Rasterize to be off. How would one retrieve the actual displayed Grid (or Column, or any other complex construct for that matter) size, as displayed, not as re-interpreted in a new Cell? $\endgroup$ Commented Oct 30, 2013 at 4:22
  • $\begingroup$ AFAIK currently there is no such way in general. Sometimes CurrentValue with Dynamic allows to get information you seek but there are only few specific cases (search for "currentvalue" this site). One workaround would be to select the object in question by hands and then copy it as "Bitmap" (under Windows). Then you can investigate this raster image directly. I also suspect that your real problem is just ill-posed and there may be a way to achieve what you want without such functionality. $\endgroup$ Commented Oct 30, 2013 at 5:48
  • $\begingroup$ Copy as Bitmap can be done programmatically, and while it will destroy clipboard contents, this is actually not a bad, however crude it may be, solution. One would simply have to write the ridiculously complex selection routine that will select exact content you want to measure, then copy as MGF and work with image properties of that image, then empty the clipboard. Alternatively, it's probably possible to cache what was in clipboard before and then put it back, unless it's some really complex content. $\endgroup$ Commented Oct 30, 2013 at 6:11
  • $\begingroup$ Yes, all these is possible. Another alternative would be to copy entire Cell somewhere in the same Notebook, then convert it to Bitmap and to assign the Bitmap to some variable for further investigation, then delete the duplicate Cell. But I think there should be only very rare cases when all of this is really necessary. If you are sure that you problem cannot be solved in different way, it is worth to create separate question (and it is good idea to express the real problem, because currently I cannot imagine what it may be...). $\endgroup$ Commented Oct 30, 2013 at 6:28
0
$\begingroup$

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}. ??

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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