Convert .bmp to ByteArray

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

I think you could do it using GetPixels() and put it into a Color[ ].

1 Like

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.

Just like Texture has GetPixels:

It also has GetPixels32:

And the nice thing is that even if it’s a bmp, png, jpg, gif, etc… it’ll read the pixels without you having to resolve the file format.

2 Likes

Great, thanks for your explanations.