Unity 6.3
0 онлайн 30 гостей 3 в системе
Вход

ImageConversion.EncodeToJPG

Declaration

public static byte[] EncodeToJPG(Texture2D tex, int quality);
public static byte[] EncodeToJPG(Texture2D tex);

Параметры

Параметр Описание
Текст Текстура текста для преобразования.
качество Качество JPG для кодирования. Диапазон от 1 до 100. 1 — самое низкое качество. По умолчанию 75.

Описание

Кодирует текстуру в формат JPG.

Эта функция возвращает массив байтов, который представляет собой данные файла JPG. Вы можете сохранить кодированные данные в виде файла или отправить их по сети без дальнейшей обработки.

Эта функция не работает в любом сжатом формате. Texture.isReadable должны быть true. Кодированные данные JPG будут либо 8-битными оттенками серого, RGB или RGBA (в зависимости от переданного формата).

// Saves screenshot as JPG file.
using UnityEngine;
using System.Collections;
using System.IO;

public class JPGScreenSaver : MonoBehaviour { // Take a shot immediately IEnumerator Start() { yield return SaveScreenJPG(); }

IEnumerator SaveScreenJPG() { // Read the screen buffer after rendering is complete yield return new WaitForEndOfFrame();

// Create a texture in RGB24 format the size of the screen int width = Screen.width; int height = Screen.height; Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

// Read the screen contents into the texture tex.ReadPixels(new Rect(0, 0, width, height), 0, 0); tex.Apply();

// Encode the texture in JPG format byte[] bytes = ImageConversion.EncodeToJPG(tex); Object.Destroy(tex);

// Write the returned byte array to a file in the project folder File.WriteAllBytes(Application.dataPath + "/../SavedScreen.jpg", bytes); } }

Дополнительные ресурсы: ,EncodeArrayToJPG,, ,EncodeNativeArrayToJPG,, ,EncodeToPNG,, ,EncodeToTGA,, ,EncodeToEXR,.