using StgSharp.Math; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace StgSharp.Graphics { public class Image { [StructLayout(LayoutKind.Sequential)] internal struct ImageInfo { internal int width; internal int height; internal int channel; internal IntPtr pixelPtr; } internal byte[] pixelArray; internal ImageInfo info; public int Width { get => info.width; } public int Height { get => info.height; } public int Channel { get => info.channel; } internal Image(ImageInfo information) { info = information; int length = info.width * info.height * info.channel; pixelArray = new byte[length]; Marshal.Copy( source: info.pixelPtr, destination: pixelArray, startIndex: 0, length: length ); } internal byte[] PixelArray { get { if (pixelArray == null) { pixelArray = new byte[info.width * info.height * info.channel]; Marshal.Copy(source: info.pixelPtr, destination: pixelArray, startIndex: 0, length: info.width * info.height * info.channel); Marshal.FreeHGlobal(info.pixelPtr); } return pixelArray; } } public unsafe Image() { #if NET5_0_OR_GREATER Unsafe.SkipInit(out pixelArray); #endif info = new ImageInfo(); } ~Image() { Marshal.FreeHGlobal(info.pixelPtr); } } }