This code works:
var image = transform.GetComponent<Image>();
var bits = Util.LoadBinary(BoardImage); // load off disk in binary
var tex = new Texture2D(2, 2); // size not used
image.preserveAspect = true;
image.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
But it only works for PNG and JPG, not for BMP. Unity Editor can load BMP, but it creates metadata. So it can be done, I just don’t know how.
So the question is how to load a BMP file directly in binary using the API, not via the editor.
Besides what @Seyren said in his answer you can always load any format you want as long as you know how the format is specified and as long as it’s not too complex / complicated.
This is both true for the BMP format. All you actually need is the wikipedia page of the BMP format. It explains the whole format.
Another option besides creating your own BMP loader is to search the net for a C# implementation of a BMP loader. If you don’t mind setting your project to .NET 2.0 and importing the System.Drawing assembly, you could use the Bitmap class inside System.Drawing. Though it might not work on certain platforms and is quite large.
A bit of “googling” revealed this. Not sure what language that actually is, but the code is in C# and looks like a naive straight forward implementation of the wikipedia information ^^. It just extracts the image data as a byte array as well as the image resolution. It certainly is a start to implement a proper BMP → Texture2D loader.
Since the BMP format is rather simple i just went ahead and quickly wrote a BMPFile loader for Unity. It’s hosted on my pastebin account as unlisted paste.
As i have written in the information header I have not tested it with all possible file versions. Also it doesn’t support any of the compressed formats (yet). Though it can read alpha values if required. I couldn’t test the 16 and 32 bit formats since MS Paint only supports up to 24 bit images. Paint .NET only supports 8 or 24bit. So i only tested the alpha read for indexed images. I actually changed the alpha value manually in the file with a hexeditor. I also tried to change the palette colors of a monochrome image. Funnily the windows image preview as well as my loader had no problem with a monochrome image (1bit) that has a custom palette (for example red and green). MS Paint itself doesn’t seem to actually interpret the palette and force the image to a B&W image when loaded.
// at the top
BMPLoader loader = new BMPLoader();
//loader.ForceAlphaReadWhenPossible = true; // can be uncomment to read alpha
//load the image data
BMPImage img = loader.LoadBMP("C:\\Data\\B1.bmp");
// Convert the Color32 array into a Texture2D
Texture2D tex = img.ToTexture2D();
If someone uses it and / or finds any bugs or problems, feel free to leave a comment. If you try to load an unsupported format it should print some error messages in the console.
For convenience the LoadBMP method has overloads that takes either a filepath as string, a data stream, a byte array or a BinaryReader.
If the API does not support it, you either have to wait until it does, or just go back to PNG or JPG, which in my opinion is the best option for a videogame.
BMP files are uncompressed, and will take a lot of space, and therefore, more time to load, which is not good.
I would choose in this case, between PNG and JPG, a PNG file, since it uses lossless compression, which means the image will not lose any data compared to the original, so you can freely switch to it, being the size the only difference between the two formats.
If your concern is, for example, that you have a lot of files in BMP and it’s a pain to convert them to PNG, there is a lot of batch conversors you can find on google.
I know that this is not the solution you would want, but in my opinion BMP is not a good idea.
Not to mention PNG supports transparency.