3

I am trying to convert a RawImage in a array bytes (bytes[]) but the RawImage don't have a encondePNG or something else to get a bytes for a RawImage, any idea how I can get the array of bytes?

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);



}
Programmer
  • 121,791
  • 22
  • 236
  • 328

1 Answers1

6

RawImage is just a component rendering a Texture assigned to its texture property. To get the byte array, you need to access that Texture first then cast it to Texture2D.

Your RawImage component:

public RawImage rawImage;

Get the Texture it is rendering then cast it to Texture2D:

Texture2D rawImageTexture = (Texture2D)rawImage.texture;

Obtain the byte array as png or jpeg:

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

If you want the uncompressed data of the RawImage:

Color32[] rawData = rawImageTexture.GetPixels32();

To also convert the the Color32[] to byte array see this post.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Nothing to add. Just out of interrest I did something like that shortly (downloaded a PNG via http to texture and back to PNG and reuploaded it). The Png file growed from 860kb to 1.02Mb so I guess it is not compressed? How could we archieve the same compression level on PNG ? – derHugo Nov 08 '18 at 19:41
  • That's interesting. When I said "uncompressed data", I meant the one loaded in and represented as in Texture data format which is obtained by `GetPixels32(),`. The size of GetPixels32(), is more than EncodeToPNG() by a lot. I suspect that the loaded data is bigger because Unity has to store more information about the texture than just the actual size. Also, there is mipmaps being applied to the loaded texture. We can't tell without the source code of Unity. – Programmer Nov 08 '18 at 19:59
  • No I totally agree with you. I just thought maybe you came across this as well but maybe I should open a question for that? – derHugo Nov 08 '18 at 20:04
  • Sure you can. I think that would probably require someone that works at Unity to answer that. There are few on this site but I am curios too on this subject and would like to know what's going on – Programmer Nov 08 '18 at 20:07