Beispiel #1
0
        public static unsafe Image LoadFromMemory(IntPtr pSource, int size, bool makeACopy, GCHandle?handle)
        {
            var stream = new BinarySerializationReader(new NativeMemoryStream((byte *)pSource, size));

            // Read and check magic code
            var magicCode = stream.ReadUInt32();

            if (magicCode != MagicCode)
            {
                return(null);
            }

            // Read header
            var imageDescription = new ImageDescription();

            ImageDescriptionSerializer.Serialize(ref imageDescription, ArchiveMode.Deserialize, stream);

            if (makeACopy)
            {
                var buffer = Utilities.AllocateMemory(size);
                Utilities.CopyMemory(buffer, pSource, size);
                pSource   = buffer;
                makeACopy = false;
            }

            var image = new Image(imageDescription, pSource, 0, handle, !makeACopy);

            var totalSizeInBytes = stream.ReadInt32();

            if (totalSizeInBytes != image.TotalSizeInBytes)
            {
                throw new InvalidOperationException("Image size is different than expected.");
            }

            // Read image data
            stream.Serialize(image.DataPointer, image.TotalSizeInBytes);

            return(image);
        }