Skip to content

Instantly share code, notes, and snippets.

@lruckman
Last active February 16, 2018 22:54
Show Gist options
  • Save lruckman/fefbe4c02c6e8e1d7d68a18dc1128814 to your computer and use it in GitHub Desktop.
Save lruckman/fefbe4c02c6e8e1d7d68a18dc1128814 to your computer and use it in GitHub Desktop.
ImageSharp Resize with ResizeMode.Crop
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.Primitives;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApp8
{
class Program
{
static async Task Main(string[] args)
{
using (var httpClient = new HttpClient())
{
var response = await httpClient.GetAsync("https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png")
.ConfigureAwait(false);
using (var inputStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
using (var image = Image.Load(inputStream))
{
CropImage(image, new Size(25, 50))
.Save(@"C:\devnoscan\foo.png");
}
}
}
}
private static Image<Rgba32> CropImage(Image<Rgba32> sourceImage, Size size)
{
return sourceImage.Clone(
ctx => ctx.Resize(
new ResizeOptions
{
Size = size,
Mode = ResizeMode.Crop
}));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment