【问题标题】:Bitmap from byte[] array来自 byte[] 数组的位图
【发布时间】:2015-05-24 12:56:17
【问题描述】:

我想从一个字节[] 创建一个位图。我的问题是我不能在 Unity 中使用 BitmapSource,如果我使用 MemoryStream Unity 会出错。

我试过了:

  Bitmap bitmap = new Bitmap(512, 424);

  var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size),
  ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

  Marshal.Copy(arrayData, 0, data.Scan0, arrayData.Length);

  bitmap.UnlockBits(data);

它有效,但我得到的位图是错误的方式。有人可以解释一下原因并为我找到解决方案吗?

【问题讨论】:

  • 你的位图垂直镜像了吗?

标签: unity3d bitmap bytearray


【解决方案1】:

这可能是两件事,也许是结合起来的:choice of coordinate systemEndianness

有一个约定(我相信是通用的)从左到右列出像素,但没有关于 vertical 方向的约定。虽然一些程序和 API 的 Y 坐标在底部为零并向上增加,但其他程序和 API 则完全相反。我不知道你从哪里得到byte[],但有些API 允许你在写入、读取或使用纹理时配置像素方向。否则,您将不得不手动重新排列行。

这同样适用于字节顺序; ARGB 有时意味着蓝色是最后一个字节,有时是第一个字节。一些类,如 BitConverter 也有内置解决方案。

Unity 使用 big-endianbottom-up 纹理。事实上,Unity handles lots of this stuff under the hood,并且在导入位图文件时必须重新排序行和翻转字节。 Unity 还提供了LoadImageEncodeToPNG 等方法来解决这两个问题。

为了说明byte[] 会发生什么,此示例代码以三种不同的方式保存相同的图像(但您需要将它们导入为 Truecolor 以在 Unity 中正确查看它们):

using UnityEngine;
using UnityEditor;
using System.Drawing;
using System.Drawing.Imaging;

public class CreateTexture2D : MonoBehaviour {

    public void Start () {

        int texWidth = 4, texHeight = 4;

        // Raw 4x4 bitmap data, in bottom-up big-endian ARGB byte order. It's transparent black for the most part.
        byte[] rawBitmap = new byte[] {

            // Red corner (bottom-left) is written first
            255,255,0,0,  0,0,0,0,      0,0,0,0,      0,0,0,0,
            0,0,0,0,      0,0,0,0,      0,0,0,0,      0,0,0,0,
            0,0,0,0,      0,0,0,0,      0,0,0,0,      0,0,0,0,
            255,0,0,255,  255,0,0,255,  255,0,0,255,  255,0,0,255
            //Blue border (top) is the last "row" of the array
        };

        // We create a Texture2D from the rawBitmap
        Texture2D texture = new Texture2D(texWidth, texHeight, TextureFormat.ARGB32, false);
        texture.LoadRawTextureData(rawBitmap);
        texture.Apply();

        // 1.- We save it directly as a Unity asset (btw, this is useful if you only use it inside Unity)
        UnityEditor.AssetDatabase.CreateAsset(texture, "Assets/TextureAsset.asset");

        // 2.- We save the texture to a file, but letting Unity handle formatting
        byte[] textureAsPNG = texture.EncodeToPNG();
        System.IO.File.WriteAllBytes(Application.dataPath + "/EncodedByUnity.png", textureAsPNG);

        // 3.- Rearrange the rawBitmap manually into a top-down small-endian ARGB byte order. Then write to a Bitmap, and save to disk.
        // Bonus: This permutation is it's own inverse, so it works both ways.
        byte[] rearrangedBM = new byte[rawBitmap.Length];
        for (int row = 0; row < texHeight; row++)
            for (int col = 0; col < texWidth; col++)
                for (int i = 0; i < 4; i++)
                    rearrangedBM[row * 4 * texWidth + 4 * col + i] = rawBitmap[(texHeight - 1 - row) * 4 * texWidth + 4 * col + (3 - i)];

        Bitmap bitmap = new Bitmap(texWidth, texHeight, PixelFormat.Format32bppArgb);
        var data = bitmap.LockBits(new Rectangle(0, 0, texWidth, texHeight), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
        System.Runtime.InteropServices.Marshal.Copy(rearrangedBM, 0, data.Scan0, rearrangedBM.Length);
        bitmap.UnlockBits(data);
        bitmap.Save(Application.dataPath + "/SavedBitmap.png", ImageFormat.Png);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多