Как получить закодированное изображение (png, jpg и т. Д.) Из Texture2D? - PullRequest
0 голосов
/ 23 мая 2019

Ниже приведен код, который я пытаюсь изменить.

static byte[] GetImageAsByteArray(string imageFilePath)
{
    // Open a read-only file stream for the specified file.
    using (FileStream fileStream =
        new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
    {
        // Read the file's contents into a byte array.
        BinaryReader binaryReader = new BinaryReader(fileStream);
        return binaryReader.ReadBytes((int)fileStream.Length);
    }
}

Как видите, этот код использует путь к файлу. Я хочу передать изображение Texture2D в метод, как показано ниже:

static byte[] GetImageAsByteArray(Texture2D image)

Как получить тот же вывод, что и для метода файлового потока, но с использованием texture2d. Ниже моя попытка сделать это:

static byte[] GetImageAsByteArray(Texture2D image)
{
    /* Open a read-only file stream for the specified file.
    using (FileStream fileStream =
        new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
        */
    {
        // Read the file's contents into a byte array.
        Debug.Log("MADE IT TO GETIMAGESBYTEARR");
       return binaryReader.ReadBytes(image.GetRawTextureData());

    }
}

1 Ответ

0 голосов
/ 23 мая 2019

Используйте метод ImageConversion в зависимости от требуемой кодировки.

Texture2D image;    

// Encodes this texture into EXR format using ZIP compression
byte[] exrEncoded = ImageConversion.EncodeToEXR (image, Texture2D.EXRFlags.CompressZIP);

// Encodes this texture into JPG format
byte[] jpgEncoded = ImageConversion.EncodeToJPG (image);

// Encodes this texture into JPG format
byte[] pngEncoded = EncodeToPNG(image);

// Encodes this texture into TGA format
byte[] tgaEncoded = EncodeToTGA(image);

Чтобы использовать их, необходимо установить флаг разрешения на чтение / запись текстуры вtrue в настройках импорта текстур.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...