1

I want to load a bitmap from disk. I am using the Bitmap class in System.Drawing, which makes it very simple.

However, when I deploy to macOS, I get an exception.

DllNotFoundException: /Users/bokken/build/ouput/Unity-Technologies/mono/external/buildscripts/add_to_build_results/monodistribution/lib/libgdiplus.dylib

Is there a simple way to load bitmaps that doesn't create this error, or alternately, how do I fix this error?

Anthony Burg
  • 570
  • 4
  • 20

2 Answers2

0

Unity is cross platform, but C# .NET Framework is not.

So to keep your game cross platform, you either use other cross platform libraries (somewhere published on the Internet), or stick to capabilities of Unity.

Unity provides, for example a Texture2D class, that can be a workaround. Some more info from this thread

Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
  • 1
    I am using Texture2Ds internally, and the bitmap after being loaded is converted into a Texture2D. I simply need to load the bitmap from disk. – Anthony Burg Jun 24 '20 at 20:33
  • Bitmap is nothing but an array of bytes, and Texture2D gives an opportunity to load it. Follow the link I posted. `System.Drawing.Bitmap` class is Windows only – Alexey S. Larionov Jun 24 '20 at 20:50
  • 1
    I've followed the link, but it's not clear at all to me how to apply that to the question. – Anthony Burg Jun 24 '20 at 21:01
  • You cannot make a `System.Drawing.Bitmap`, unless you're on Windows. You can use `Texture2D.GetPixels32` of Unity to get `Color32[]` which is essentially a bitmap (a data structure, not `System.Drawing.Bitmap`). You can use this array for your needs (transform it into another object, or a `byte[]` or whatever. – Alexey S. Larionov Jun 24 '20 at 21:31
  • 1
    Yes, but how do I load the bitmap from disk into the Texture2D to begin with? GetPixels32() is used once the texture has the data. – Anthony Burg Jun 24 '20 at 21:44
  • @AnthonyBurg no hard to search [for it](http://answers.unity.com/answers/802424/view.html) either – Alexey S. Larionov Jun 24 '20 at 21:58
  • 1
    That link is to loading a PNG or JPG, not a bitmap. – Anthony Burg Jun 24 '20 at 21:59
0

The solution is to use custom code to load a bitmap.

See here How can I use a .bmp file and create a Texture in Unity at runtime? which solves this problem.

Note that the BMPLoader in the accepted answer doesn't draw mirror image bitmaps correctly, and the code probably should be tested thoroughly for other possible problems.

Surprised Unity doesn't have a built-in capability to load images outside of PNG and JPG.

Anthony Burg
  • 570
  • 4
  • 20