Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

byte[] to Texture2D ?

Discussion in 'Scripting' started by rom, Dec 1, 2007.

  1. rom

    rom

    Joined:
    Jul 2, 2006
    Posts:
    265
    Hi all,
    I am trying to convert a byte array to a Texture2D
    I need to do the reverse of EncodeToPNG.

    Any Ideas here.
    I am using C#
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Convert the byte[] array to a Color[] array and use SetPixels, perhaps?

    --Eric
     
  3. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    The only way to decode a PNG currently short of implementing your own PNG encoder or making a plugin is to get the PNG using the WWW class. I did file a bug asking for DecodePNGFromBytes() and they listened, it just didn't happen yet is all.

    Cheers,
    -Jon
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you have already decoded the PNG data to uncompressed RGBA, then I think Eric is probably right - colours are stored as floats, so you would need to convert the stored integer data.

    If you mean that you have compressed PNG data (eg, read from a file) then you might find the attached files useful. The PNG decoder they implement is limited to 8-bit RGBA data (it can't handled indexed or monochrome PNGs or images with no alpha bytes) and is not battle tested but it might be enough to get you started.

    However, it delivers the pixel data as RGBA bytes so you will still have to do what Eric described.

    (By the way, the decoder relies on the SharpZip library, available from http://icsharpcode.net/OpenSource/SharpZipLib)
     

    Attached Files:

  5. LorenzoChomp

    LorenzoChomp

    Joined:
    Aug 26, 2009
    Posts:
    14
    Sorry to revive this old thread, but I couldn't find a more recent one about this very same topic.

    Is there now a method to DecodePNG? Included in Unity, I mean.

    Thanks in advance.

    EDIT: hey, wait a minute... silly of me. There is Texture2D.LoadImage(byte[] bytes) to achieve this. Sorry for the inconveniences!
     
  6. unicoea

    unicoea

    Joined:
    Feb 18, 2013
    Posts:
    60
  7. Andrey-Postelzhuk

    Andrey-Postelzhuk

    Joined:
    Nov 26, 2013
    Posts:
    75
  8. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,834
    From which Unity version ImageConversion was introduced?
     
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,150
    Unity 2017.1.
     
    ModLunar likes this.