mirror of https://github.com/SixLabors/ImageSharp
11 changed files with 136 additions and 358 deletions
@ -1,98 +0,0 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="ImageExtensions.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// <summary>
|
|||
// Encapsulates a series of time saving extension methods to the <see cref="T:System.Drawing.Imaging.Image" /> class.
|
|||
// </summary>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Core.Common.Extensions |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Drawing; |
|||
using System.Drawing.Imaging; |
|||
using ImageProcessor.Imaging; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates a series of time saving extension methods to the <see cref="T:System.Drawing.Imaging.Image" /> class.
|
|||
/// </summary>
|
|||
public static class ImageExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Returns information about the given <see cref="System.Drawing.Image"/>.
|
|||
/// </summary>
|
|||
/// <param name="image">
|
|||
/// The image to extend.
|
|||
/// </param>
|
|||
/// <param name="format">
|
|||
/// The image format.
|
|||
/// </param>
|
|||
/// <param name="fetchFrames">
|
|||
/// Whether to fetch the images frames.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The <see cref="ImageInfo"/>.
|
|||
/// </returns>
|
|||
public static ImageInfo GetImageInfo(this Image image, ImageFormat format, bool fetchFrames = true) |
|||
{ |
|||
ImageInfo info = new ImageInfo |
|||
{ |
|||
Height = image.Height, |
|||
Width = image.Width, |
|||
// ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
|
|||
IsIndexed = (image.PixelFormat & PixelFormat.Indexed) != 0 |
|||
}; |
|||
|
|||
if (image.RawFormat.Guid == ImageFormat.Gif.Guid && format.Guid == ImageFormat.Gif.Guid) |
|||
{ |
|||
if (ImageAnimator.CanAnimate(image)) |
|||
{ |
|||
info.IsAnimated = true; |
|||
|
|||
if (fetchFrames) |
|||
{ |
|||
FrameDimension frameDimension = new FrameDimension(image.FrameDimensionsList[0]); |
|||
int frameCount = image.GetFrameCount(frameDimension); |
|||
int last = frameCount - 1; |
|||
int delay = 0; |
|||
int index = 0; |
|||
List<GifFrame> gifFrames = new List<GifFrame>(); |
|||
|
|||
for (int f = 0; f < frameCount; f++) |
|||
{ |
|||
int thisDelay = BitConverter.ToInt32(image.GetPropertyItem(20736).Value, index); |
|||
int toAddDelay = thisDelay * 10 < 20 ? 20 : thisDelay * 10; // Minimum delay is 20 ms
|
|||
|
|||
// Find the frame
|
|||
image.SelectActiveFrame(frameDimension, f); |
|||
|
|||
// TODO: Get positions.
|
|||
gifFrames.Add(new GifFrame { Delay = toAddDelay, Image = (Image)image.Clone() }); |
|||
|
|||
// Reset the position.
|
|||
if (f == last) |
|||
{ |
|||
image.SelectActiveFrame(frameDimension, 0); |
|||
} |
|||
|
|||
delay += toAddDelay; |
|||
index += 4; |
|||
} |
|||
|
|||
info.GifFrames = gifFrames; |
|||
info.AnimationLength = delay; |
|||
|
|||
// Loop info is stored at byte 20737.
|
|||
info.LoopCount = BitConverter.ToInt16(image.GetPropertyItem(20737).Value, 0); |
|||
info.IsLooped = info.LoopCount != 1; |
|||
} |
|||
} |
|||
} |
|||
|
|||
return info; |
|||
} |
|||
} |
|||
} |
|||
@ -1,151 +0,0 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="ImageFormatExtensions.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// <summary>
|
|||
// Encapsulates a series of time saving extension methods to the <see cref="T:System.Drawing.Imaging.ImageFormat" />
|
|||
// class.
|
|||
// </summary>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Core.Common.Extensions |
|||
{ |
|||
#region
|
|||
|
|||
using System.Drawing.Imaging; |
|||
using System.Linq; |
|||
|
|||
#endregion
|
|||
|
|||
/// <summary>
|
|||
/// Encapsulates a series of time saving extension methods to the <see cref="T:System.Drawing.Imaging.ImageFormat" /> class.
|
|||
/// </summary>
|
|||
public static class ImageFormatExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the correct mime-type for the given <see cref="T:System.Drawing.Imaging.ImageFormat" />.
|
|||
/// </summary>
|
|||
/// <param name="imageFormat">The <see cref="T:System.Drawing.Imaging.ImageFormat" />.</param>
|
|||
/// <returns>The correct mime-type for the given <see cref="T:System.Drawing.Imaging.ImageFormat" />.</returns>
|
|||
public static string GetMimeType(this ImageFormat imageFormat) |
|||
{ |
|||
if (imageFormat.Equals(ImageFormat.Icon)) |
|||
{ |
|||
return "image/x-icon"; |
|||
} |
|||
|
|||
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); |
|||
return codecs.First(codec => codec.FormatID == imageFormat.Guid).MimeType; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the name for the given <see cref="T:System.Drawing.Imaging.ImageFormat" />.
|
|||
/// </summary>
|
|||
/// <param name="format">
|
|||
/// The <see cref="T:System.Drawing.Imaging.ImageFormat" /> to get the name for.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The <see cref="string"/> representing the name of the <see cref="T:System.Drawing.Imaging.ImageFormat" />.
|
|||
/// </returns>
|
|||
public static string GetName(this ImageFormat format) |
|||
{ |
|||
if (format.Guid == ImageFormat.MemoryBmp.Guid) |
|||
{ |
|||
return "MemoryBMP"; |
|||
} |
|||
|
|||
if (format.Guid == ImageFormat.Bmp.Guid) |
|||
{ |
|||
return "Bmp"; |
|||
} |
|||
|
|||
if (format.Guid == ImageFormat.Emf.Guid) |
|||
{ |
|||
return "Emf"; |
|||
} |
|||
|
|||
if (format.Guid == ImageFormat.Wmf.Guid) |
|||
{ |
|||
return "Wmf"; |
|||
} |
|||
|
|||
if (format.Guid == ImageFormat.Gif.Guid) |
|||
{ |
|||
return "Gif"; |
|||
} |
|||
|
|||
if (format.Guid == ImageFormat.Jpeg.Guid) |
|||
{ |
|||
return "Jpeg"; |
|||
} |
|||
|
|||
if (format.Guid == ImageFormat.Png.Guid) |
|||
{ |
|||
return "Png"; |
|||
} |
|||
|
|||
if (format.Guid == ImageFormat.Tiff.Guid) |
|||
{ |
|||
return "Tiff"; |
|||
} |
|||
|
|||
if (format.Guid == ImageFormat.Exif.Guid) |
|||
{ |
|||
return "Exif"; |
|||
} |
|||
|
|||
if (format.Guid == ImageFormat.Icon.Guid) |
|||
{ |
|||
return "Icon"; |
|||
} |
|||
|
|||
return "[ImageFormat: " + format.Guid + "]"; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the correct file extension for the given <see cref="T:System.Drawing.Imaging.ImageFormat"/>.
|
|||
/// </summary>
|
|||
/// <param name="imageFormat">
|
|||
/// The <see cref="T:System.Drawing.Imaging.ImageFormat"/> to return the extension for.
|
|||
/// </param>
|
|||
/// <param name="originalExtension">
|
|||
/// The original Extension.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The correct file extension for the given <see cref="T:System.Drawing.Imaging.ImageFormat"/>.
|
|||
/// </returns>
|
|||
public static string GetFileExtension(this ImageFormat imageFormat, string originalExtension) |
|||
{ |
|||
string name = imageFormat.GetName(); |
|||
|
|||
switch (name) |
|||
{ |
|||
case "Icon": |
|||
return ".ico"; |
|||
case "Gif": |
|||
return ".gif"; |
|||
case "Bmp": |
|||
return ".bmp"; |
|||
case "Png": |
|||
return ".png"; |
|||
case "Tiff": |
|||
if (!string.IsNullOrWhiteSpace(originalExtension) && originalExtension.ToUpperInvariant() == ".TIF") |
|||
{ |
|||
return ".tif"; |
|||
} |
|||
|
|||
return ".tiff"; |
|||
case "Jpeg": |
|||
if (!string.IsNullOrWhiteSpace(originalExtension) && originalExtension.ToUpperInvariant() == ".JPG") |
|||
{ |
|||
return ".jpg"; |
|||
} |
|||
|
|||
break; |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
@ -1,78 +0,0 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="ImageUtils.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// <summary>
|
|||
// Encapsulates useful image utility methods.
|
|||
// </summary>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Imaging |
|||
{ |
|||
#region Using
|
|||
|
|||
using System; |
|||
using System.Drawing; |
|||
using System.Drawing.Imaging; |
|||
using System.Reflection; |
|||
|
|||
#endregion
|
|||
|
|||
/// <summary>
|
|||
/// Encapsulates useful image utility methods.
|
|||
/// </summary>
|
|||
public static class ImageUtils |
|||
{ |
|||
/// <summary>
|
|||
/// Returns an instance of EncodingParameters for jpeg compression.
|
|||
/// </summary>
|
|||
/// <param name="quality">The quality to return the image at.</param>
|
|||
/// <returns>The encodingParameters for jpeg compression. </returns>
|
|||
public static EncoderParameters GetEncodingParameters(int quality) |
|||
{ |
|||
EncoderParameters encoderParameters = null; |
|||
try |
|||
{ |
|||
// Create a series of encoder parameters.
|
|||
encoderParameters = new EncoderParameters(1); |
|||
|
|||
// Set the quality.
|
|||
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality); |
|||
} |
|||
catch |
|||
{ |
|||
if (encoderParameters != null) |
|||
{ |
|||
encoderParameters.Dispose(); |
|||
} |
|||
} |
|||
|
|||
return encoderParameters; |
|||
} |
|||
|
|||
/// <summary>Returns a value indicating whether or not the given bitmap is indexed.</summary>
|
|||
/// <param name="image">The image to check</param>
|
|||
/// <returns>Whether or not the given bitmap is indexed.</returns>
|
|||
public static bool IsIndexed(Image image) |
|||
{ |
|||
// Test value of flags using bitwise AND.
|
|||
// ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
|
|||
return (image.PixelFormat & PixelFormat.Indexed) != 0; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Uses reflection to allow the creation of an instance of <see cref="PropertyItem"/>.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// The <see cref="PropertyItem"/>.
|
|||
/// </returns>
|
|||
public static PropertyItem CreatePropertyItem() |
|||
{ |
|||
Type type = typeof(PropertyItem); |
|||
ConstructorInfo constructor = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, new Type[] { }, null); |
|||
|
|||
return (PropertyItem)constructor.Invoke(null); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue