Last active
February 16, 2018 22:54
-
-
Save lruckman/fefbe4c02c6e8e1d7d68a18dc1128814 to your computer and use it in GitHub Desktop.
ImageSharp Resize with ResizeMode.Crop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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