mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
3.0 KiB
68 lines
3.0 KiB
// <copyright file="Crop.cs" company="James Jackson-South">
|
|
// Copyright (c) James Jackson-South and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
// </copyright>-------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace ImageProcessorCore
|
|
{
|
|
using Processors;
|
|
|
|
/// <summary>
|
|
/// Extension methods for the <see cref="Image"/> type.
|
|
/// </summary>
|
|
public static partial class ImageExtensions
|
|
{
|
|
/// <summary>
|
|
/// Crops an image to the given width and height.
|
|
/// </summary>
|
|
/// <param name="source">The image to resize.</param>
|
|
/// <param name="width">The target image width.</param>
|
|
/// <param name="height">The target image height.</param>
|
|
/// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
|
|
/// <returns>The <see cref="ImageProcessorCore.Image"/></returns>
|
|
public static Image Crop(this Image source, int width, int height, ProgressEventHandler progressHandler = null)
|
|
{
|
|
return Crop(source, width, height, source.Bounds, progressHandler);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Crops an image to the given width and height with the given source rectangle.
|
|
/// <remarks>
|
|
/// If the source rectangle is smaller than the target dimensions then the
|
|
/// area within the source is resized performing a zoomed crop.
|
|
/// </remarks>
|
|
/// </summary>
|
|
/// <param name="source">The image to crop.</param>
|
|
/// <param name="width">The target image width.</param>
|
|
/// <param name="height">The target image height.</param>
|
|
/// <param name="sourceRectangle">
|
|
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
|
/// </param>
|
|
/// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
|
|
/// <returns>The <see cref="Image"/></returns>
|
|
public static Image Crop(this Image source, int width, int height, Rectangle sourceRectangle, ProgressEventHandler progressHandler = null)
|
|
{
|
|
Guard.MustBeGreaterThan(width, 0, nameof(width));
|
|
Guard.MustBeGreaterThan(height, 0, nameof(height));
|
|
|
|
if (sourceRectangle.Width < width || sourceRectangle.Height < height)
|
|
{
|
|
// If the source rectangle is smaller than the target perform a
|
|
// cropped zoom.
|
|
source = source.Resize(sourceRectangle.Width, sourceRectangle.Height);
|
|
}
|
|
|
|
CropProcessor processor = new CropProcessor();
|
|
processor.OnProgress += progressHandler;
|
|
|
|
try
|
|
{
|
|
return source.Process(width, height, sourceRectangle, new Rectangle(0, 0, width, height), processor);
|
|
}
|
|
finally
|
|
{
|
|
processor.OnProgress -= progressHandler;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|