# How to get the TCastleImageTransform sizes

**URL:** <https://forum.castle-engine.io/t/how-to-get-the-tcastleimagetransform-sizes/1422>\
**Category:** Uncategorized\
**Created:** [November 7, 2024, 9:04pm UTC](https://forum.castle-engine.io/t/how-to-get-the-tcastleimagetransform-sizes/1422 "2024-11-07T21:04:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noscow](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/noscow/32/964_2.png) [@Noscow](https://forum.castle-engine.io/u/Noscow)\
**Post date:** [November 7, 2024, 9:04pm UTC](https://forum.castle-engine.io/t/how-to-get-the-tcastleimagetransform-sizes/1422/1 "2024-11-07T21:04:31Z")

</div>

Good day!  
I load images (\*.png) with native sizes (`Size := Vector2(0, 0)`).  
How can I get their width and height in pixels?  
(I need this information for sprites alignment on the scene)

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [November 9, 2024, 2:26am UTC](https://forum.castle-engine.io/t/how-to-get-the-tcastleimagetransform-sizes/1422/2 "2024-11-09T02:26:56Z")

</div>

You can look at [LocalBoundingBox](https://castle-engine.io/apidoc/html/CastleTransform.TCastleTransform.html#LocalBoundingBox) of the `TCastleImageTransform` instance (defined for every `TCastleTransform`).

When you left the `Size` as zero, then the `LocalBoundingBox` will reflect exactly the loaded image size. So, something like this should be OK:

```delphi
var
  Box: TBox3D;
begin
  Box := MyImageTransform.LocalBoundingBox;
  ImageWidth := Boxes.SizeX;
  ImageHeight := Boxes.SizeY;
  // .. do what you need with ImageWidth, ImageHeight
end;

```

Notes:

- `Boxes.SizeX` and `Boxes.SizeY` are `Single`. If you need to act on integers, then in this case it is OK to just round them, i.e. `ImageWidth := Round(Boxes.SizeX)`. It will be OK, the underlying numbers are really integers in this case.
- While this is efficient and universal answer, I understand it is not obvious 🙂 Let me know if this very counter-intuitive. Internally, we know the real image size, and we _could_ add to `TCastleImageTransform` properties like `ImageWidth, ImageHeight:Cardinal` that would maybe be more straightforward to use. Comments welcome.

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [November 9, 2024, 2:41am UTC](https://forum.castle-engine.io/t/how-to-get-the-tcastleimagetransform-sizes/1422/3 "2024-11-09T02:41:32Z")

</div>

> [@michalis](#):
>
> While this is efficient and universal answer, I understand it is not obvious 🙂 Let me know if this very counter-intuitive. Internally, we know the real image size, and we _could_ add to `TCastleImageTransform` properties like `ImageWidth, ImageHeight:Cardinal` that would maybe be more straightforward to use. Comments welcome.

Nevermind, I decided myself that `TCastleImageTransform.ImageWidth` and `TCastleImageTransform.ImageHeight` would be more intuitive. They would make answer to this question more obvious 🙂 So, added them to CGE: [Add TCastleImageTransform.ImageWidth/Height · castle-engine/castle-engine@f287d3f · GitHub](https://github.com/castle-engine/castle-engine/commit/f287d3f547035b9a91e2df5894a2e46db985020d) .

---

<div class="post-metadata">

**Author:** ![Noscow](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/noscow/32/964_2.png) [@Noscow](https://forum.castle-engine.io/u/Noscow)\
**Post date:** [November 9, 2024, 10:11pm UTC](https://forum.castle-engine.io/t/how-to-get-the-tcastleimagetransform-sizes/1422/4 "2024-11-09T22:11:56Z")

</div>

Thanks, it works fine.  
But I still have small problem. I making 2.5D game and use 2D sprites in 3D. So, if two NPC or monsters, for example, have different sizes, I need set their Translation.Y as zero plus half of their heights. In this case they will stand at one line.

Next, sizes is zero, but all sprites are scaled to correct displaying in 3D viewport (so, `Scale` is about 0.05… It is important addition I forgot to say).  
It means, when I use `Translation.Y = 0 + Box.SizeY / 2`, very small (scaled) sprite get `Translation.Y` more then 100 and ups to the sky 🚀 ⛅

What can you recommend in this case? How can I set `Translation` in same measures as scaled `ImageTransform`? I tried to find the multiplier empirically but failed 🙃

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [November 12, 2024, 4:17pm UTC](https://forum.castle-engine.io/t/how-to-get-the-tcastleimagetransform-sizes/1422/5 "2024-11-12T16:17:35Z")

</div>

> [@Noscow](#):
>
> What can you recommend in this case? How can I set `Translation` in same measures as scaled `ImageTransform`? I tried to find the multiplier empirically but failed 🙃

I recommend to create a hierarchy of transformations in this case. Basically, you don’t have to specify all translations/rotations/scaling on one TCastleTransform instance ( `TCastleImageTransform` is also `TCastleTransform` instance).

E.g.

- MyImageTransform (`TCastleImageTransform`):
  - leave Scale as default (1 1 1)
  - and change only `Translation`, e.g. set `MyImageTransform.Translation` to `Vector3(0, -MyImageTransform.ImageHeight / 2, 0)`.

- Make `MyImageTransform` a child of `MyScaledSprite` (TCastleTransform)
  - and on `MyScaledSprite` set non-default scale like `Vector3(0.1,0.1,0.1)` if need.

---

<div class="post-metadata">

**Author:** ![Noscow](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/noscow/32/964_2.png) [@Noscow](https://forum.castle-engine.io/u/Noscow)\
**Post date:** [November 12, 2024, 8:15pm UTC](https://forum.castle-engine.io/t/how-to-get-the-tcastleimagetransform-sizes/1422/6 "2024-11-12T20:15:18Z")

</div>

Great, thanks! It is fixed, finally 😊
