AGG GAA
AGG GAA

Reputation: 63

How get native image size at runtime?

I need to scale the plane with the height and width of the image, so the maintexture of the plane will not stretch. like this:

    void LoadBG()
{
    string bgpaht = Application.persistentDataPath +"/a.png";
    if (File.Exists(bgpath))
    {
        byte[] bgbyte=File.ReadAllBytes(bgpath);
        ...
        ...
        Texture2D t2d = new Texture2D(a.width,a.height); 
        t2d.LoadImage(bgbyte);
        plane.transform.localScale = new Vector3(t2d.width,1.0f,t2d.height);
        plane.renderer.material.mainTexture=t2d;

    }
}

OK,now how can I get the width and height of the image "a.png"?

Upvotes: 0

Views: 1093

Answers (1)

Gianni B.
Gianni B.

Reputation: 2731

Have you had a look at the Image class?

https://msdn.microsoft.com/en-us/library/System.Drawing.Image(v=vs.110).aspx

It has the properties Height and Width.

Just load the image like this

Image yourImage = Image.FromFile(pathOfYourFile);

Upvotes: 1

Related Questions