Home > other >  Unity - Get Bytes array from Image
Unity - Get Bytes array from Image

Time:05-07

I am wondering how I am able to get an array of Bytes from a standard PNG image.

The purpose is to be able to send information from Unity, to cloudstore.

Thank you!

CodePudding user response:

This link help you to this: ImageConversion.EncodeToPNG

public Texture2D tex;

public void Convert()
{
    byte[] bytes = tex.EncodeToPNG();
    
    // do something..
} 

CodePudding user response:

Edit: realized this might not work for the question, because it looks like OP is trying to convert a unity texture to a byte array, instead of a file.

Just use File.ReadAllBytes (https://docs.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes?view=net-6.0).

Example:

using System;
using System.IO;

public class example
{
   public byte[] ex(string path) 
   {
       byte​[] ​imageData​ ​=​ ​File​.​ReadAllBytes​(​path​);
       return imageData;
   }
}
  • Related