/// <summary> /// The WebP save function. /// </summary> /// <param name="writeImageCallback">The callback used to write the WebP image.</param> /// <param name="input">The input surface.</param> /// <param name="parameters">The encode parameters.</param> /// <param name="metadata">The image metadata.</param> /// <param name="callback">The progress callback.</param> /// <exception cref="ArgumentNullException"><paramref name="writeImageCallback"/> is null. /// or /// <paramref name="input"/> is null.</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 WebPSave( Surface input, Stream output, EncodeParams parameters, MetadataParams metadata, WebPReportProgress callback) { if (input == null) { throw new ArgumentNullException(nameof(input)); } StreamIOHandler handler = new StreamIOHandler(output); WebPWriteImage writeImageCallback = handler.WriteImageCallback; WebPEncodingError retVal = WebPEncodingError.Ok; if (IntPtr.Size == 8) { retVal = WebP_64.WebPSave(writeImageCallback, input.Scan0.Pointer, input.Width, input.Height, input.Stride, parameters, metadata, callback); } else { retVal = WebP_32.WebPSave(writeImageCallback, input.Scan0.Pointer, input.Width, input.Height, input.Stride, parameters, metadata, callback); } GC.KeepAlive(writeImageCallback); if (retVal != WebPEncodingError.Ok) { switch (retVal) { case WebPEncodingError.OutOfMemory: case WebPEncodingError.BitStreamOutOfMemory: throw new OutOfMemoryException(Resources.InsufficientMemoryOnSave); case WebPEncodingError.FileTooBig: throw new WebPException(Resources.EncoderFileTooBig); case WebPEncodingError.ApiVersionMismatch: throw new WebPException(Resources.ApiVersionMismatch); case WebPEncodingError.MetadataEncoding: throw new WebPException(Resources.EncoderMetadataError); case WebPEncodingError.UserAbort: throw new OperationCanceledException(); case WebPEncodingError.BadDimension: throw new WebPException(Resources.InvalidImageDimensions); case WebPEncodingError.NullParameter: throw new WebPException(Resources.EncoderNullParameter); case WebPEncodingError.InvalidConfiguration: throw new WebPException(Resources.EncoderInvalidConfiguration); case WebPEncodingError.PartitionZeroOverflow: throw new WebPException(Resources.EncoderPartitionZeroOverflow); case WebPEncodingError.PartitionOverflow: throw new WebPException(Resources.EncoderPartitionOverflow); case WebPEncodingError.BadWrite: if (handler.WriteException != null) { throw new IOException(Resources.EncoderBadWrite, handler.WriteException); } else { throw new IOException(Resources.EncoderBadWrite); } default: throw new WebPException(Resources.EncoderGenericError); } } }