gpt4 book ai didi

c# - 如何将 UI RawImage 转换为字节数组

转载 作者:太空宇宙 更新时间:2023-11-03 22:39:20 24 4
gpt4 key购买 nike

我正在尝试将 RawImage 转换为数组字节 (bytes[]),但 RawImage 没有 encondePNG 或其他东西来获取 RawImage 的字节,知道如何获取字节数组吗?

public class RegistryScreen : UIScreen
{

Texture2D pickedImage;
public RawImage[] getRawImageProfile;

public void ChangeIconImage()
{
PickImageFromGallery();

//WebService
mygetImageProfileRequestData = new getImageProfileRequestData();

//this is the problem
mygetImageProfileRequestData.image = getRawImageProfile[0].texture;

}

public void PickImageFromGallery(int maxSize = 1024)
{
NativeGallery.GetImageFromGallery((path) =>
{
if (path != null)
{
// Create Texture from selected image
pickedImage = NativeGallery.LoadImageAtPath(path, maxSize);

////Sust. texture in image(Sprite)
for (int i = 0; i < getRawImageProfile.Length; i++)
{
getRawImageProfile[i].texture = pickedImage;
}
}


Debug.Log("getRawImage: " + getRawImageProfile[0].texture);

}, maxSize: maxSize);



}

最佳答案

RawImage 只是一个渲染分配给其纹理属性的 Texture 的组件。要获取字节数组,您需要先访问该 Texture,然后将其转换为 Texture2D

您的 RawImage 组件:

public RawImage rawImage;

获取正在渲染的纹理,然后将其转换为 Texture2D:

Texture2D rawImageTexture = (Texture2D)rawImage.texture;

获取 png 或 jpeg 格式的字节数组:

byte[] pngData = rawImageTexture.EncodeToPNG();
byte[] jpegData = rawImageTexture.EncodeToJPG();

如果你想要 RawImage 的未压缩数据:

Color32[] rawData = rawImageTexture.GetPixels32();

要同时将 Color32[] 转换为字节数组,请参阅 this发布。

关于c# - 如何将 UI RawImage 转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53213537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com