- Home /
Convert System.Drawing.Bitmap to Texture2D
I am using a 3rd party library that works with Bitmaps to do some image extraction from a custom model format. The library currently uses System.Drawing to handle the bitmaps but I've run into trouble getting that particular dll to work on OSX. I decided that it would make the most sense to just replace the Bitmap code with Texture2D code but I'm hitting a block with texture formats.
the 3rd party code uses two types of pixel formats: PixelFormat.Format32bppArgb
and PixelFormat.Format8bppIndexed
. I'm not sure what the Unity TextureFormat
values for these are. I've played around a lot and finally got ARGB32 to work but the colors are all shades of blue instead of the desired colors. Other formats result in overreads.
The code I'm trying to convert looks like this: if (width % 4 == 0) { var handle = GCHandle.Alloc(imageData, GCHandleType.Pinned); bitmap = new Bitmap(width, height, width, PixelFormat.Format8bppIndexed, handle.AddrOfPinnedObject()); handle.Free(); } else { bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed); var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); var data = bitmap.LockBits(rect, ImageLockMode.WriteOnly, bitmap.PixelFormat); for (int h = 0; h < data.Height; h++) { Marshal.Copy(imageData, h * data.Width, IntPtr.Add(data.Scan0, h * data.Stride), data.Width); } bitmap.UnlockBits(data); } return bitmap;
How can I get rid of Bitmap
and just use Texture2D?