public unsafe static Image LoadFromMemory(IntPtr pSource, int size, bool makeACopy, GCHandle?handle) { using (var memoryStream = new UnmanagedMemoryStream((byte *)pSource, size)) using (var bitmap = (Bitmap)BitmapFactory.DecodeStream(memoryStream)) { var bitmapData = bitmap.LockPixels(); var image = Image.New2D(bitmap.Width, bitmap.Height, 1, PixelFormat.B8G8R8A8_UNorm, 1, bitmap.RowBytes); #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES // Directly load image as RGBA instead of BGRA, because OpenGL ES devices don't support it out of the box (extension). CopyMemoryBGRA(image.PixelBuffer[0].DataPointer, bitmapData, image.PixelBuffer[0].BufferStride); #else Utilities.CopyMemory(image.PixelBuffer[0].DataPointer, bitmapData, image.PixelBuffer[0].BufferStride); #endif bitmap.UnlockPixels(); if (handle != null) { handle.Value.Free(); } else if (!makeACopy) { Utilities.FreeMemory(pSource); } return(image); } }
public unsafe static Image LoadFromMemory(IntPtr pSource, int size, bool makeACopy, GCHandle?handle) { using (var memoryStream = new UnmanagedMemoryStream((byte *)pSource, size)) using (var bitmap = (Bitmap)System.Drawing.Image.FromStream(memoryStream)) { var sourceArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height); // Lock System.Drawing.Bitmap var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); var image = Image.New2D(bitmap.Width, bitmap.Height, 1, PixelFormat.B8G8R8A8_UNorm, 1, bitmapData.Stride); // var dataRect = new DataRectangle(bitmapData.Stride, bitmapData.Scan0); try { #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES && SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP // Directly load image as RGBA instead of BGRA, because OpenGL ES devices don't support it out of the box (extension). image.Description.Format = PixelFormat.R8G8B8A8_UNorm; CopyMemoryBGRA(image.PixelBuffer[0].DataPointer, bitmapData.Scan0, image.PixelBuffer[0].BufferStride); #else Utilities.CopyMemory(image.PixelBuffer[0].DataPointer, bitmapData.Scan0, image.PixelBuffer[0].BufferStride); #endif } finally { bitmap.UnlockBits(bitmapData); if (handle != null) { handle.Value.Free(); } else if (!makeACopy) { Utilities.FreeMemory(pSource); } } return(image); } }
public static Image LoadFromMemory(IntPtr pSource, int size, bool makeACopy, GCHandle?handle) { using (var imagedata = NSData.FromBytes(pSource, (uint)size)) using (var cgImage = new UIImage(imagedata).CGImage) { var image = Image.New2D((int)cgImage.Width, (int)cgImage.Height, 1, PixelFormat.R8G8B8A8_UNorm, 1, (int)cgImage.BytesPerRow); using (var context = new CGBitmapContext(image.PixelBuffer[0].DataPointer, cgImage.Width, cgImage.Height, 8, cgImage.Width * 4, cgImage.ColorSpace, CGBitmapFlags.PremultipliedLast)) { context.DrawImage(new RectangleF(0, 0, cgImage.Width, cgImage.Height), cgImage); if (handle != null) { handle.Value.Free(); } else if (!makeACopy) { Utilities.FreeMemory(pSource); } return(image); } } }