/// <summary> /// Loads the image to process. Always call this method first. /// </summary> /// <param name="stream"> /// The <see cref="T:System.IO.Stream"/> containing the image information. /// </param> /// <returns> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public ImageFactory Load(Stream stream) { ISupportedImageFormat format = FormatUtilities.GetFormat(stream); if (format == null) { throw new ImageFormatException("Input stream is not a supported format."); } MemoryStream memoryStream = new MemoryStream(); // Copy the stream. Disposal of the input stream is the responsibility // of the user. stream.CopyTo(memoryStream); // Set the position to 0 afterwards. if (stream.CanSeek) { stream.Position = 0; } memoryStream.Position = 0; // Set our image as the memory stream value. this.Image = format.Load(memoryStream); // Store the stream so we can dispose of it later. this.InputStream = memoryStream; // Set the other properties. format.Quality = DefaultQuality; format.IsIndexed = FormatUtilities.IsIndexed(this.Image); IQuantizableImageFormat imageFormat = format as IQuantizableImageFormat; if (imageFormat != null) { imageFormat.ColorCount = FormatUtilities.GetColorCount(this.Image); } this.backupFormat = format; this.CurrentImageFormat = format; // Always load the data. // TODO. Some custom data doesn't seem to get copied by default methods. foreach (int id in this.Image.PropertyIdList) { this.ExifPropertyItems[id] = this.Image.GetPropertyItem(id); } this.ShouldProcess = true; // Normalize the gamma component of the image. if (this.FixGamma) { this.Gamma(2.2F); } return(this); }
/// <summary> /// Loads the image to process from an array of bytes. Always call this method first. /// </summary> /// <param name="bytes"> /// The <see cref="T:System.Byte"/> containing the image information. /// </param> /// <returns> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public ImageFactory Load(byte[] bytes) { MemoryStream memoryStream = new MemoryStream(bytes); ISupportedImageFormat format = FormatUtilities.GetFormat(memoryStream); if (format == null) { throw new ImageFormatException("Input stream is not a supported format."); } // Set our image as the memory stream value. this.Image = format.Load(memoryStream); // Store the stream so we can dispose of it later. this.InputStream = memoryStream; // Set the other properties. format.Quality = DefaultQuality; format.IsIndexed = FormatUtilities.IsIndexed(this.Image); IQuantizableImageFormat imageFormat = format as IQuantizableImageFormat; if (imageFormat != null) { imageFormat.ColorCount = FormatUtilities.GetColorCount(this.Image); } this.backupFormat = format; this.CurrentImageFormat = format; // Always load the data. foreach (int id in this.Image.PropertyIdList) { this.ExifPropertyItems[id] = this.Image.GetPropertyItem(id); } this.ShouldProcess = true; // Normalize the gamma component of the image. if (this.FixGamma) { this.Gamma(2.2F); } return(this); }
/// <summary> /// Loads the image to process. Always call this method first. /// </summary> /// <param name="imagePath">The absolute path to the image to load.</param> /// <returns> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public ImageFactory Load(string imagePath) { FileInfo fileInfo = new FileInfo(imagePath); if (fileInfo.Exists) { this.ImagePath = imagePath; // Open a file stream to prevent the need for lock. using (FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read)) { ISupportedImageFormat format = FormatUtilities.GetFormat(fileStream); if (format == null) { throw new ImageFormatException("Input stream is not a supported format."); } MemoryStream memoryStream = new MemoryStream(); // Copy the stream. fileStream.CopyTo(memoryStream); // Set the position to 0 afterwards. memoryStream.Position = 0; // Set our image as the memory stream value. this.Image = format.Load(memoryStream); // Store the stream so we can dispose of it later. this.InputStream = memoryStream; // Set the other properties. format.Quality = DefaultQuality; format.IsIndexed = FormatUtilities.IsIndexed(this.Image); IQuantizableImageFormat imageFormat = format as IQuantizableImageFormat; if (imageFormat != null) { imageFormat.ColorCount = FormatUtilities.GetColorCount(this.Image); } this.backupFormat = format; this.CurrentImageFormat = format; // Always load the data. foreach (PropertyItem propertyItem in this.Image.PropertyItems) { this.ExifPropertyItems[propertyItem.Id] = propertyItem; } this.ShouldProcess = true; // Normalize the gamma component of the image. if (this.FixGamma) { this.Gamma(2.2F); } } } else { throw new FileNotFoundException(imagePath); } return(this); }