コード例 #1
0
        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);
        }
コード例 #2
0
ファイル: WebPFile.cs プロジェクト: paintdotnet/pdn-webp
        /// <summary>
        /// The WebP save function.
        /// </summary>
        /// <param name="input">The input Document.</param>
        /// <param name="output">The output Stream.</param>
        /// <param name="quality">The WebP save quality.</param>
        /// <param name="preset">The WebP encoding preset.</param>
        /// <param name="keepMetadata"><c>true</c> if metadata should be preserved; otherwise <c>false</c>.</param>
        /// <param name="scratchSurface">The scratch surface.</param>
        /// <param name="progressCallback">The progress callback.</param>
        /// <exception cref="FormatException">The image exceeds 16383 pixels in width and/or height.</exception>
        /// <exception cref="OutOfMemoryException">Insufficient memory to save the image.</exception>
        /// <exception cref="WebPException">The encoder returned a non-memory related error.</exception>
        internal static void Save(
            Document input,
            Stream output,
            int quality,
            WebPPreset preset,
            bool keepMetadata,
            Surface scratchSurface,
            ProgressEventHandler progressCallback)
        {
            if (input.Width > WebPNative.WebPMaxDimension || input.Height > WebPNative.WebPMaxDimension)
            {
                throw new FormatException(Resources.InvalidImageDimensions);
            }

            WebPNative.EncodeParams encParams = new WebPNative.EncodeParams
            {
                quality = quality,
                preset  = preset
            };

            using (RenderArgs ra = new RenderArgs(scratchSurface))
            {
                input.Render(ra, true);
            }

            WebPNative.MetadataParams metadata = null;
            if (keepMetadata)
            {
                metadata = CreateWebPMetadata(input, scratchSurface);
            }

            WebPNative.WebPReportProgress encProgress = null;

            if (progressCallback != null)
            {
                encProgress = delegate(int percent)
                {
                    try
                    {
                        progressCallback(null, new ProgressEventArgs(percent, true));
                        return(true);
                    }
                    catch (OperationCanceledException)
                    {
                        return(false);
                    }
                };
            }

            WebPNative.WebPSave(scratchSurface, output, encParams, metadata, encProgress);
        }