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.
77 lines
2.7 KiB
77 lines
2.7 KiB
// --------------------------------------------------------------------------------------------------------------------
|
|
// <copyright file="Program.cs" company="James South">
|
|
// Copyright (c) James South.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
// </copyright>
|
|
// <summary>
|
|
// The program.
|
|
// </summary>
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace ImageProcessorConsole
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using ImageProcessor;
|
|
|
|
/// <summary>
|
|
/// The program.
|
|
/// </summary>
|
|
public class Program
|
|
{
|
|
/// <summary>
|
|
/// The main routine.
|
|
/// </summary>
|
|
/// <param name="args">
|
|
/// The args.
|
|
/// </param>
|
|
public static void Main(string[] args)
|
|
{
|
|
string path = new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath;
|
|
|
|
// ReSharper disable once AssignNullToNotNullAttribute
|
|
string resolvedPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\input"));
|
|
DirectoryInfo di = new DirectoryInfo(resolvedPath);
|
|
if (!di.Exists)
|
|
{
|
|
di.Create();
|
|
}
|
|
|
|
//IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".gif");
|
|
IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".gif", ".webp", ".bmp", ".jpg", ".png");
|
|
|
|
foreach (FileInfo fileInfo in files)
|
|
{
|
|
byte[] photoBytes = File.ReadAllBytes(fileInfo.FullName);
|
|
|
|
// ImageProcessor
|
|
using (MemoryStream inStream = new MemoryStream(photoBytes))
|
|
{
|
|
using (ImageFactory imageFactory = new ImageFactory(true))
|
|
{
|
|
Size size = new Size(200, 200);
|
|
|
|
// Load, resize, set the format and quality and save an image.
|
|
imageFactory.Load(inStream)
|
|
.Constrain(size)
|
|
.Save(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\output", fileInfo.Name)));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<FileInfo> GetFilesByExtensions(DirectoryInfo dir, params string[] extensions)
|
|
{
|
|
if (extensions == null)
|
|
{
|
|
throw new ArgumentNullException("extensions");
|
|
}
|
|
|
|
IEnumerable<FileInfo> files = dir.EnumerateFiles();
|
|
return files.Where(f => extensions.Contains(f.Extension, StringComparer.OrdinalIgnoreCase));
|
|
}
|
|
}
|
|
}
|
|
|