来自byte []数组的位图

时间:2015-05-24 12:56:17

标签: unity3d bitmap bytearray

我想从byte []创建一个位图。我的问题是我无法在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);

它可以工作,但我得到的位图是错误的方式。有人可以解释我为什么并为我找到解决方案吗?

1 个答案:

答案 0 :(得分:4)

这可能是两件事,可能合并:choice of coordinate systemEndianness

有一种惯例(我相信是通用的)从左到右列出像素,但没有关于垂直方向的内容。虽然一些程序和API的Y坐标在底部为零并且向上增加,但其他程序和API完全相反。我不知道从哪里获得byte[],但是一些API允许您在编写,阅读或使用纹理时配置像素方向。否则,您必须手动重新排列行。

这同样适用于字节顺序; ARGB有时意味着Blue是最后一个字节,有时候是第一个字节。有些类,如BitConverter也有buit-in解决方案。

Unity使用 big-endian bottom-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);
    }
}