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
  4. Dismiss Notice

Convert .bmp to ByteArray

Discussion in 'Scripting' started by sadlig, Aug 19, 2020.

  1. sadlig

    sadlig

    Joined:
    Aug 19, 2020
    Posts:
    3
    Hello,
    I'm trying to import a .bmp image to use it as a byte array, to generate tiles with the pixels data contained in the image. I can use an int array too, because I just need a way to access the RGB data for each pixel.
    I've found several codes to do this conversion ; one uses ImageConverter, and another uses image.Save(stream) and stream.ToArray(). But both of them seem to be unavailable because Unity doesn't recognize System.Drawing.Imaging (even if it recognizes System.Drawing).
    So I've tried to add a reference to System.Drawing.Imaging in visual studio, but I've got only System.Drawing and System.Drawing.Design as references to add. So I've tried to import/install it, but it seems that the NuGet package manager doesn't work (nothing happens when I click on "gerer les packages NuGet" in the references of my project).
    I'm new to Unity, so I've maybe missed an easier way to access the pixel data from a .bmp file, but many researches led me to this way. Thanks for any help
    Capture d’écran (3).png
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    I think you could do it using GetPixels() and put it into a Color[].
     
    sadlig likes this.
  3. sadlig

    sadlig

    Joined:
    Aug 19, 2020
    Posts:
    3
    This seems to function correctly, thanks ! I prefer using a Color32[] however, to perfectly detect each color of the image without having to rely on intervals with floats. Sorry for the previous unnecessary long post.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    PraetorBlue and sadlig like this.
  5. sadlig

    sadlig

    Joined:
    Aug 19, 2020
    Posts:
    3
    Great, thanks for your explanations.