【问题标题】:Get image width and height from a byte array data从字节数组数据中获取图像宽度和高度
【发布时间】:2017-05-02 12:48:00
【问题描述】:

我有一个 byte[] 数组,它读取一个本地文件,然后我将它分配给一个统一的 Texture2D。使用 im.sprite = Sprite.Create() 函数分配图像。

我遇到的问题是我需要从字节数组中检测图像的宽度和高度,以便调整纹理大小并避免在运行时在统一场景中拉伸图像。

您知道我如何使用某种库或函数从图像字节数组中创建一个临时图像,然后在创建精灵并应用纹理之前查看所需的宽度和高度吗?

我在 Java 中找到了解决方案,但无法通过在 c# 中进行调整来使其正常工作。

【问题讨论】:

标签: c# arrays image unity3d


【解决方案1】:

Texture2D.LoadImage被调用时,它会自动将Texture2D实例替换为图片的大小。然后,您可以从 Texture2D 实例中获取宽度和高度,并将其传递给 Sprite.Create 函数。

将图像加载到字节数组

byte[] imageBytes = loadImage(savePath);

创建新的 Texture2D

Texture2D texture = new Texture2D(2, 2);

将其加载到 Texture2D 中。 调用 LoadImage 后,2x2 将替换为该图像的大小。

texture.LoadImage(imageBytes);

你的图片组件

Image im = GetComponent<Image>();

你可以用它创建一个新的Rect,然后将它传递给你的Sprite函数来创建新的Sprite

im.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));

详细了解Texture2D.LoadImage 函数here

您只需要Texture2D.widthTexture2D.height 变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-31
    • 2014-07-15
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 2017-04-30
    • 2016-06-04
    • 2015-09-04
    相关资源
    最近更新 更多