'Unity - Get Bytes array from Image [duplicate]

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!



Solution 1:[1]

This link help you to this: ImageConversion.EncodeToPNG

public Texture2D tex;

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KiynL
Solution 2 QwertyHJKL1234