/// <summary> /// Performs compression on the specified file. With some formats the image will be decoded /// and encoded and this will result in a small quality reduction. If the new file size is not /// smaller the file won't be overwritten. /// </summary> /// <param name="fileName">The file name of the image to compress.</param> /// <returns>True when the image could be compressed otherwise false.</returns> public bool Compress(string fileName) { var filePath = FileHelper.CheckForBaseDirectory(fileName); Throw.IfNullOrEmpty(nameof(fileName), filePath); return(DoCompress(new FileInfo(filePath))); }
/// <summary> /// Performs compression on the specified the file. With some formats the image will be decoded /// and encoded and this will result in a small quality reduction. If the new file size is not /// smaller the file won't be overwritten. /// </summary> /// <param name="fileName">The file name of the image to compress</param> public void Compress(string fileName) { string filePath = FileHelper.CheckForBaseDirectory(fileName); Throw.IfNullOrEmpty(nameof(fileName), filePath); DoCompress(new FileInfo(filePath)); }
/// <summary> /// Writes the images to the specified file name. If the output image's file format does not /// allow multi-image files multiple files will be written. /// </summary> /// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception> public Task WriteAsync(string fileName, CancellationToken cancellationToken) { var filePath = FileHelper.CheckForBaseDirectory(fileName); Throw.IfNullOrEmpty(nameof(fileName), filePath); return(WriteAsync(new FileInfo(filePath), cancellationToken)); }
///<summary> /// Returns the format information of the specified format based on the extension of the /// file. If that fails the format will be determined by 'pinging' the file. ///</summary> ///<param name="fileName">The name of the file to get the format for.</param> public static MagickFormatInfo GetFormatInformation(string fileName) { string filePath = FileHelper.CheckForBaseDirectory(fileName); Throw.IfInvalidFileName(filePath); return(GetFormatInformation(new FileInfo(filePath))); }
/// <summary> /// Returns the format information. The extension of the supplied file name is used to /// determine the format. /// </summary> /// <param name="fileName">The name of the file to check.</param> /// <returns>The format information.</returns> public static MagickFormatInfo Create(string fileName) { string filePath = FileHelper.CheckForBaseDirectory(fileName); Throw.IfNullOrEmpty(nameof(fileName), filePath); return(Create(new FileInfo(filePath))); }
/// <summary> /// Performs lossless compression on the specified file. If the new file size is not smaller /// the file won't be overwritten. /// </summary> /// <param name="fileName">The file name of the image to compress</param> public void LosslessCompress(string fileName) { string filePath = FileHelper.CheckForBaseDirectory(fileName); Throw.IfInvalidFileName(filePath); DoLosslessCompress(new FileInfo(filePath)); }
private static string CheckDirectory(string path) { Throw.IfNullOrEmpty("path", path); path = FileHelper.CheckForBaseDirectory(path); path = Path.GetFullPath(path); Throw.IfFalse("path", Directory.Exists(path), "Unable to find directory: {0}", path); return(path); }
///<summary> /// Read basic information about an image with multiple frames/pages. ///</summary> ///<param name="fileName">The fully qualified name of the image file, or the relative image file name.</param> ///<exception cref="MagickException"/> public static IEnumerable <MagickImageInfo> ReadCollection(string fileName) { string filePath = FileHelper.CheckForBaseDirectory(fileName); foreach (Wrapper.MagickImageInfo info in Wrapper.MagickImageInfo.ReadCollection(filePath)) { yield return(new MagickImageInfo(info)); } }
///<summary> /// Initializes a new instance of the ImageProfile class. ///</summary> ///<param name="name">The name of the profile.</param> ///<param name="fileName">The fully qualified name of the profile file, or the relative profile file name.</param> public ImageProfile(string name, string fileName) { Throw.IfNullOrEmpty(nameof(name), name); Name = name; string filePath = FileHelper.CheckForBaseDirectory(fileName); Data = File.ReadAllBytes(filePath); }
///<summary> /// Initializes a new instance of the ImageProfile class. ///</summary> ///<param name="name">The name of the profile.</param> ///<param name="fileName">The fully qualified name of the profile file, or the relative profile file name.</param> public ImageProfile(string name, string fileName) { Throw.IfNullOrEmpty("name", name); Name = name; string filePath = FileHelper.CheckForBaseDirectory(fileName); Data = Wrapper.MagickReader.Read(filePath); }
/// <summary> /// Writes the image to the specified file name. /// </summary> /// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param> /// <param name="format">The format to use.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception> public Task WriteAsync(string fileName, MagickFormat format, CancellationToken cancellationToken) { var filePath = FileHelper.CheckForBaseDirectory(fileName); Throw.IfNullOrEmpty(nameof(fileName), filePath); var bytes = ToByteArray(format); return(File.WriteAllBytesAsync(filePath, bytes, cancellationToken)); }
///<summary> /// Read all image frames. ///</summary> ///<param name="fileName">The fully qualified name of the image file, or the relative image file name.</param> ///<param name="readSettings">The settings to use when reading the image.</param> ///<exception cref="MagickException"/> public void Read(string fileName, MagickReadSettings readSettings) { Clear(); string filePath = FileHelper.CheckForBaseDirectory(fileName); foreach (Wrapper.MagickImage image in _Instance.Read(filePath, readSettings)) { _Images.Add(MagickImage.Create(image)); } }
/// <summary> /// Initializes a new instance of the <see cref="MagickScript"/> class. /// </summary> /// <param name="fileName">The fully qualified name of the script file, or the relative script file name.</param> public MagickScript(string fileName) { string filePath = FileHelper.CheckForBaseDirectory(fileName); Throw.IfNullOrEmpty(nameof(fileName), filePath); using (FileStream stream = File.OpenRead(filePath)) { Initialize(stream); } }
/// <summary> /// Read single image frame. /// </summary> /// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param> /// <param name="readSettings">The settings to use when reading the image.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception> public async Task ReadAsync(string fileName, IMagickReadSettings <QuantumType>?readSettings, CancellationToken cancellationToken) { var filePath = FileHelper.CheckForBaseDirectory(fileName); Throw.IfNullOrEmpty(nameof(fileName), filePath); var bytes = await File.ReadAllBytesAsync(fileName, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); Read(bytes, readSettings, false, fileName); }
/// <summary> /// Read single image frame from pixel data. /// </summary> /// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param> /// <param name="settings">The pixel settings to use when reading the image.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception> public async Task ReadPixelsAsync(string fileName, IPixelReadSettings <QuantumType>?settings, CancellationToken cancellationToken) { var filePath = FileHelper.CheckForBaseDirectory(fileName); Throw.IfNullOrEmpty(nameof(fileName), filePath); var data = await File.ReadAllBytesAsync(filePath, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); ReadPixels(data, 0, data.Length, settings); }
private void AddImages(string fileName, MagickReadSettings readSettings, bool ping) { string filePath = FileHelper.CheckForBaseDirectory(fileName); Throw.IfInvalidFileName(filePath); MagickSettings settings = CreateSettings(readSettings); settings.FileName = filePath; settings.Ping = ping; IntPtr result = _NativeInstance.ReadFile(settings); AddImages(result, settings); }
///<summary> /// Writes the images to the specified file name. If the output image's file format does not /// allow multi-image files multiple files will be written. ///</summary> ///<param name="fileName">The fully qualified name of the image file, or the relative image file name.</param> ///<exception cref="MagickException"/> public void Write(string fileName) { string filePath = FileHelper.CheckForBaseDirectory(fileName); if (_Images.Count == 0) { return; } MagickSettings settings = _Images[0].Settings; settings.FileName = fileName; try { AttachImages(); _NativeInstance.WriteFile(_Images[0], settings); } finally { DetachImages(); } }
/// <summary> /// Writes the images to the specified file name. If the output image's file format does not /// allow multi-image files multiple files will be written. /// </summary> /// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param> /// <param name="format">The format to use.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception> public async Task WriteAsync(string fileName, MagickFormat format, CancellationToken cancellationToken) { var filePath = FileHelper.CheckForBaseDirectory(fileName); Throw.IfNullOrEmpty(nameof(fileName), filePath); if (_images.Count == 0) { return; } try { AttachImages(); var bytes = ToByteArray(format); await File.WriteAllBytesAsync(filePath, bytes, cancellationToken).ConfigureAwait(false); } finally { DetachImages(); } }
///<summary> /// Read basic information about an image. ///</summary> ///<param name="fileName">The fully qualified name of the image file, or the relative image file name.</param> ///<exception cref="MagickException"/> public void Read(string fileName) { string filePath = FileHelper.CheckForBaseDirectory(fileName); _Instance.Read(filePath); }
///<summary> /// Writes the images to the specified file name. If the output image's file format does not /// allow multi-image files multiple files will be written. ///</summary> ///<param name="fileName">The fully qualified name of the image file, or the relative image file name.</param> ///<exception cref="MagickException"/> public void Write(string fileName) { string filePath = FileHelper.CheckForBaseDirectory(fileName); _Instance.Write(GetImageInstances(), filePath); }