namespace X10D.Drawing
{
#region Using Directives
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Threading.Tasks;
#endregion
///
/// Extension methods for .
///
public static class ImageExtensions
{
///
/// Asynchronously converts the image so that its aspect ratio is 1:1, surrounding any new area with
/// transparency.
///
/// The image to convert.
/// Returns an .
public static async Task ToSquareAsync(this Image image) =>
await Task.Run(image.ToSquare);
///
/// Asynchronously converts the image so that its aspect ratio is 1:1, surrounding any new area with
/// transparency.
///
/// The image to convert.
/// The new size of the image, i.e. the value of both the width and height.
/// Returns an .
public static async Task ToSquareAsync(this Image image, int size) =>
await Task.Run(() => image.ToSquare(size));
///
/// Asynchronously converts the image so that its aspect ratio is 1:1, surrounding any new area with a
/// specified background color.
///
/// The image to convert.
/// The new size of the image, i.e. the value of both the width and height.
/// The background color to fill.
/// Returns an .
public static async Task ToSquareAsync(this Image image, int size, Color background) =>
await Task.Run(() => image.ToSquare(size, background));
///
/// Asynchronously converts the image so that its aspect ratio is 1:1, surrounding any new area with a
/// specified background color.
///
/// The image to convert.
/// The background color to fill.
/// Returns an .
public static async Task ToSquareAsync(this Image image, Color background) =>
await Task.Run(() => image.ToSquare(background));
///
/// Converts the image so that its aspect ratio is 1:1, surrounding any new area with transparency.
///
/// The image to convert.
/// Returns an .
public static Image ToSquare(this Image image) =>
image.ToSquare(Color.Transparent);
///
/// Converts the image so that its aspect ratio is 1:1, surrounding any new area with transparency.
///
/// The image to convert.
/// The new size of the image, i.e. the value of both the width and height.
/// Returns an .
public static Image ToSquare(this Image image, int size) =>
image.ToSquare(size, Color.Transparent);
///
/// Converts the image so that its aspect ratio is 1:1, surrounding any new area with a specified background
/// color.
///
/// The image to convert.
/// The background color to fill.
/// Returns an .
public static Image ToSquare(this Image image, Color background)
{
int resolution = Math.Max(image.Width, image.Height);
return image.ToSquare(resolution, background);
}
///
/// Converts the image so that its aspect ratio is 1:1, surrounding any new area with a specified background
/// color.
///
/// The image to convert.
/// The new size of the image, i.e. the value of both the width and height.
/// The background color to fill.
/// Returns an .
public static Image ToSquare(this Image image, int size, Color background)
{
int resolution = Math.Max(image.Width, image.Height);
Bitmap newImage = new Bitmap(resolution, resolution);
using (Graphics graphics = Graphics.FromImage(newImage))
{
graphics.FillRectangle(new SolidBrush(background), new Rectangle(0, 0,
resolution, resolution));
graphics.DrawImageUnscaled(image, resolution / 2 - image.Width / 2,
resolution / 2 - image.Height / 2);
}
return newImage.Scale(size, size);
}
///
/// Asynchronously scales the image.
///
/// The image to scale.
/// The new width.
/// The new height.
/// Returns a new .
public static async Task ScaleAsync(this Image image, int width, int height) =>
await Task.Run(() => image.Scale(width, height));
///
/// Asynchronously scales the image.
///
/// The image to scale.
/// The scale factor.
/// Returns a new .
public static async Task ScaleAsync(this Image image, float factor) =>
await Task.Run(() => image.Scale(factor));
///
/// Scales the image.
///
/// The image to scale.
/// The scale factor.
/// Returns a new .
public static Bitmap Scale(this Image image, float factor) =>
image.Scale((int)(image.Width * factor), (int)(image.Height * factor));
///
/// Scales the image.
///
/// The image to scale.
/// The new width.
/// The new height.
/// Returns a new .
public static Bitmap Scale(this Image image, int width, int height)
{
Rectangle destRect = new Rectangle(0, 0, width, height);
Bitmap destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (ImageAttributes wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel,
wrapMode);
}
}
return destImage;
}
}
}