public static IntPtr LoadFromMemory(byte[] sprData, FI_FORMAT type) { //读图像数据并储存在非托管内存 IntPtr sprPoint = Marshal.AllocHGlobal(sprData.Length); Marshal.Copy(sprData, 0, sprPoint, sprData.Length); //从非托管内存提交给FreeImage int sprStream = FI.OpenMemory((int)sprPoint, sprData.Length); IntPtr dib = FI.LoadFromMemory(type, sprStream, 0); //释放 Marshal.FreeHGlobal(sprPoint); FI.CloseMemory(sprStream); return(dib); }
public static bool SaveToMemory(IntPtr dib, ref byte[] sprData, FI_FORMAT type) { //保存图像到流 int newSprStream = FI.OpenMemory(0, 0); if (FI.SaveToMemory(type, dib, newSprStream, 0) == false) { return(false); } //将非托管数据重新转为托管类型 int newSprPoint = 0; int newLen = 0; FI.AcquireMemory(newSprStream, ref newSprPoint, ref newLen); sprData = new byte[newLen]; Marshal.Copy((IntPtr)newSprPoint, sprData, 0, newLen); //释放 FI.CloseMemory(newSprStream); return(true); }