From a8b7d2cee254b23caf7868577e8859f948343263 Mon Sep 17 00:00:00 2001 From: Sarah Bradley Date: Wed, 17 Apr 2024 17:45:42 -0700 Subject: Using reusable pointers for image types hope it works.. --- Bitmap.cs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Bitmap.cs b/Bitmap.cs index 7f7b9d6..8a87f7c 100644 --- a/Bitmap.cs +++ b/Bitmap.cs @@ -40,6 +40,22 @@ public unsafe class Bitmap _ => throw new Exception("Image type not supported... How did you get this error??") }; + static readonly IntPtr PngPtr = Marshal.StringToHGlobalAnsi(".png"); + static readonly IntPtr GifPtr = Marshal.StringToHGlobalAnsi(".gif"); + static readonly IntPtr JpgPtr = Marshal.StringToHGlobalAnsi(".jpg"); + static readonly IntPtr TgaPtr = Marshal.StringToHGlobalAnsi(".tga"); + static readonly IntPtr BmpPtr = Marshal.StringToHGlobalAnsi(".bmp"); + + static IntPtr ImageTypeToPointer(ImageType type) => type switch + { + ImageType.PNG => PngPtr, + ImageType.GIF => GifPtr, + ImageType.JPG => JpgPtr, + ImageType.TGA => TgaPtr, + ImageType.BMP => BmpPtr, + _ => throw new Exception("Image type not supported... How did you get this error??") + }; + public int Width { get => img.width; @@ -59,7 +75,8 @@ public unsafe class Bitmap public byte[] ToMemory(ImageType imageType = ImageType.PNG) { - byte* ptr = Raylib.ExportImageToMemory(img, ImageTypeToString(imageType), out int length); + int length; + byte* ptr = Raylib.ExportImageToMemory(img, (sbyte*)ImageTypeToPointer(imageType), &length); byte[] mem = new byte[length]; Marshal.Copy((nint)ptr, mem, 0, length); return mem; @@ -69,7 +86,7 @@ public unsafe class Bitmap { Image img; fixed (byte* ptr = &ImageMemory[0]) - img = Raylib.LoadImageFromMemory(ImageTypeToString(imageType), ptr, ImageMemory.Length); + img = Raylib.LoadImageFromMemory((sbyte*)ImageTypeToPointer(imageType), ptr, ImageMemory.Length); return new Bitmap(img); } -- cgit