public void CleanUpNativeData(IntPtr pNativeData)
        {
            unsafe
            {
                if (pNativeData != IntPtr.Zero)
                {
                    NativeMetadataParams *metadata = (NativeMetadataParams *)pNativeData;

                    if (metadata->iccProfile != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(metadata->iccProfile);
                    }

                    if (metadata->exif != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(metadata->exif);
                    }

                    if (metadata->xmp != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(metadata->xmp);
                    }

                    Marshal.FreeHGlobal(pNativeData);
                }
            }
        }
        public IntPtr MarshalManagedToNative(object ManagedObj)
        {
            if (ManagedObj == null)
            {
                return(IntPtr.Zero);
            }

            WebPNative.MetadataParams metadata = (WebPNative.MetadataParams)ManagedObj;

            IntPtr nativeStructure = Marshal.AllocHGlobal(NativeMetadataParamsSize);

            unsafe
            {
                NativeMetadataParams *nativeMetadata = (NativeMetadataParams *)nativeStructure;

                if (metadata.iccProfile != null && metadata.iccProfile.Length > 0)
                {
                    nativeMetadata->iccProfile = Marshal.AllocHGlobal(metadata.iccProfile.Length);
                    Marshal.Copy(metadata.iccProfile, 0, nativeMetadata->iccProfile, metadata.iccProfile.Length);
                    nativeMetadata->iccProfileSize = new UIntPtr((uint)metadata.iccProfile.Length);
                }
                else
                {
                    nativeMetadata->iccProfile     = IntPtr.Zero;
                    nativeMetadata->iccProfileSize = UIntPtr.Zero;
                }

                if (metadata.exif != null && metadata.exif.Length > 0)
                {
                    nativeMetadata->exif = Marshal.AllocHGlobal(metadata.exif.Length);
                    Marshal.Copy(metadata.exif, 0, nativeMetadata->exif, metadata.exif.Length);
                    nativeMetadata->exifSize = new UIntPtr((uint)metadata.exif.Length);
                }
                else
                {
                    nativeMetadata->exif     = IntPtr.Zero;
                    nativeMetadata->exifSize = UIntPtr.Zero;
                }

                if (metadata.xmp != null && metadata.xmp.Length > 0)
                {
                    nativeMetadata->xmp = Marshal.AllocHGlobal(metadata.xmp.Length);
                    Marshal.Copy(metadata.xmp, 0, nativeMetadata->xmp, metadata.xmp.Length);
                    nativeMetadata->xmpSize = new UIntPtr((uint)metadata.xmp.Length);
                }
                else
                {
                    nativeMetadata->xmp     = IntPtr.Zero;
                    nativeMetadata->xmpSize = UIntPtr.Zero;
                }
            }

            return(nativeStructure);
        }