public static void Save(this IWICImagingFactory factory, IWICBitmapSource imageSource, string fileName) { if (factory == null) { throw new ArgumentNullException("factory"); } if (imageSource == null) { throw new ArgumentNullException("imageSource"); } if (fileName == null) { throw new ArgumentNullException("fileName"); } string extension = Path.GetExtension(fileName); if (string.IsNullOrEmpty(extension)) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "File name '{0}' does not have an extension so encoder type can not be determined.", new object[] { fileName })); } Guid encoderId; if (extension.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || extension.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { encoderId = WicGuids.GUID_ContainerFormatJpeg; } else if (extension.Equals(".png", StringComparison.OrdinalIgnoreCase)) { encoderId = WicGuids.GUID_ContainerFormatPng; } else if (extension.Equals(".bmp", StringComparison.OrdinalIgnoreCase)) { encoderId = WicGuids.GUID_ContainerFormatBmp; } else { if (!extension.Equals(".tiff", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "File name '{0}' has extension {1} which we can't handle.", new object[] { fileName, extension })); } encoderId = WicGuids.GUID_ContainerFormatTiff; } IWICStream iwicstream; factory.CreateStream(out iwicstream); iwicstream.InitializeFromFilename(fileName, GenericAccess.GENERIC_WRITE); factory.Save(imageSource, iwicstream, encoderId); GraphicsInteropNativeMethods.SafeReleaseComObject(iwicstream); }
public static void Save(this IWICImagingFactory factory, IWICBitmapSource imageSource, Stream destinationStream, Guid encoderId) { if (factory == null) { throw new ArgumentNullException("factory"); } if (imageSource == null) { throw new ArgumentNullException("imageSource"); } if (destinationStream == null) { throw new ArgumentNullException("destinationStream"); } IWICStream iwicstream; factory.CreateStream(out iwicstream); iwicstream.InitializeFromIStream(new StreamWrapper(destinationStream)); factory.Save(imageSource, iwicstream, encoderId); GraphicsInteropNativeMethods.SafeReleaseComObject(iwicstream); }