【Unity】Png画像の縦横サイズを取得
2019-11-26 2020-02-02
UnityでPng画像をpathから縦横のサイズを取得する方法
Source code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.IO; using UnityEngine; /// <summary> /// png画像の縦幅と横幅を取得する /// </summary> /// <param name="path">画像ファイルのpath</param> /// <returns>幅と高さ</returns> class ExUtl { public static Vector2 GetPngSizeFromPath(string path) { FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); fs.Seek(16, SeekOrigin.Begin); byte[] buf = new byte[8]; fs.Read(buf, 0, 8); fs.Dispose(); uint width = ((uint)buf[0] << 24) | ((uint)buf[1] << 16) | ((uint)buf[2] << 8) | (uint)buf[3]; uint height = ((uint)buf[4] << 24) | ((uint)buf[5] << 16) | ((uint)buf[6] << 8) | (uint)buf[7]; Debug.Log("width = " + width + ", height = " + height); return new Vector2(width, height); } } |