// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Provides the necessary information to support jpeg images. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Imaging.Formats { using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; /// /// Provides the necessary information to support jpeg images. /// public sealed class JpegFormat : FormatBase { /// /// Gets the file headers. /// public override byte[][] FileHeaders { get { return new[] { new byte[] { 255, 216, 255 } }; } } /// /// Gets the list of file extensions. /// public override string[] FileExtensions { get { return new[] { "jpeg", "jpg" }; } } /// /// Gets the standard identifier used on the Internet to indicate the type of data that a file contains. /// public override string MimeType { get { return "image/jpeg"; } } /// /// Gets the . /// public override ImageFormat ImageFormat { get { return ImageFormat.Jpeg; } } /// /// Applies the given processor the current image. /// /// The processor delegate. /// The . public override void ApplyProcessor(Func processor, ImageFactory factory) { base.ApplyProcessor(processor, factory); // Set the property item information from any Exif metadata. // We do this here so that they can be changed between processor methods. if (factory.PreserveExifData) { foreach (KeyValuePair propertItem in factory.ExifPropertyItems) { factory.Image.SetPropertyItem(propertItem.Value); } } } /// /// Saves the current image to the specified output stream. /// /// /// The to save the image information to. /// /// The to save. /// /// The . /// public override Image Save(Stream stream, Image image) { // Jpegs can be saved with different settings to include a quality setting for the JPEG compression. // This improves output compression and quality. using (EncoderParameters encoderParameters = FormatUtilities.GetEncodingParameters(this.Quality)) { ImageCodecInfo imageCodecInfo = ImageCodecInfo.GetImageEncoders() .FirstOrDefault(ici => ici.MimeType.Equals(this.MimeType, StringComparison.OrdinalIgnoreCase)); if (imageCodecInfo != null) { image.Save(stream, imageCodecInfo, encoderParameters); } } return image; } /// /// Saves the current image to the specified file path. /// /// The path to save the image to. /// The /// to save. /// /// The . /// public override Image Save(string path, Image image) { // Jpegs can be saved with different settings to include a quality setting for the JPEG compression. // This improves output compression and quality. using (EncoderParameters encoderParameters = FormatUtilities.GetEncodingParameters(this.Quality)) { ImageCodecInfo imageCodecInfo = ImageCodecInfo.GetImageEncoders() .FirstOrDefault(ici => ici.MimeType.Equals(this.MimeType, StringComparison.OrdinalIgnoreCase)); if (imageCodecInfo != null) { image.Save(path, imageCodecInfo, encoderParameters); } } return image; } } }