Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get image size (in pixels)? Or: How to prevent stretching and keep aspect ratio?

Asked by
doomiiii 112
8 years ago
Edited 8 years ago

Is there any way to access a Decal's Texture's image size in pixels?

If not, is there any other way, to automatically center an ImageLabel in a Frame, without stretching it or messing up it's aspect ratio?

Also, the 9-tile slicing that is enabled when changing ScaleType to Slice also has a stretching effect. I cannot make use of that either.

These images are not supposed to be stretched in any way, there are a lot of them, and they all have different sizes.

0
9-tile slicing should not create a stretching affect at all if using it properly NinjoOnline 1146 — 8y
0
From what I understand, 9-tile slicing defines 9 tiles, and it will stretch the 5 tiles that are not corners to make things fit no? doomiiii 112 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

GuiObjects have a UDim2 value called Offset, for both axes. This sizes the object based on pixels, unlike the Scale value. As well as that, you can position any object in the exact center (as long as size/2 returns a whole number).

local gui = script.Parent
local image = gui:WaitForChild("Image")

image.Size = UDim2.new(0,100,0,100) -- the second value for each axis is the Offset, 100 being pixels
image.Position = UDim2.new(0.5,-50,0.5,-50) -- this utilizes both scale and offset

Here are the formulas:

Center an object sized with offset

local size = obj.Size
local offsetX = size.X.Offset/2
local offsetY = size.Y.Offset/2
obj.Position = UDim2.new(0.5,-offsetX,0.5,-offsetY)

Center an object sized with scale

local size = obj.Size
local offsetX = size.X.Scale/2
local offsetY = size.Y.Scale/2
obj.Position = UDim2.new(0.5-offsetX,0,0.5-offsetX,0)

Hope I helped!

~TDP

Ad
Log in to vote
0
Answered by
nilVector 812 Moderation Voter
8 years ago
Edited 8 years ago

Q1: Is there any way to access a Decal's Texture's image size in pixels?
A1: I'm not sure if there's exactly a way to do that (or one that I know of).

Q2: If not, is there any other way, to automatically center an ImageLabelin a Frame, without stretching it or messing up it's aspect ratio?
A2: Yes. This is actually very simple:

local frame = script.Parent -- Or wherever it is
local imageLabel = frame:WaitForChild("ImageLabel") -- Or whatever it's called
local size = imageLabel.Size

imageLabel.Position = UDim2.new(0.5, -size.X/2, 0.5, -size.Y/2)

Hope this helps! If it does, don't forget to click Accept Answer and +1 my Rep!

Answer this question