.NET Core Image Utility for SixLabors.ImageSharp Library

This small utility library requires adding the NuGet SixLabors.ImageSharp package to the .NET Core/.NET Project. This is compatible with Windows, Linux and MacOS.

It can resize an image based on the number of megapixels or length by width and preserve the aspect ratio if so desired. It rotates/flips the image based on EXIF data. This is to accommodate mobile devices.

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.Processing;
using System.IO;
using System;

namespace ImageUtil
{
	public class GetSize
	{
		public GetSize(Stream stream)
		{
			using (Image iOriginal = Image.Load(stream))
			{
				stream.Position = 0;
				Width = iOriginal.Width;
				Height = iOriginal.Height;
			}
		}
		public int Width { get; }
		public int Height { get; }
	}

	static public class Resize
	{
		static public void SaveImage(Stream imageStream, int newWidth, int newHeight, bool preserveImageRatio, Stream saveToStream)
		{
			using (Image iOriginal = Image.Load(imageStream))
			{
				imageStream.Position = 0;
				if (preserveImageRatio)
				{
					float percentWidth = newWidth / (float)iOriginal.Width;
					float percentHeight = newHeight / (float)iOriginal.Height;
					float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
					newWidth = (int)Math.Round(iOriginal.Width * percent, 0);
					newHeight = (int)Math.Round(iOriginal.Height * percent, 0);
				}
				resize(imageStream, iOriginal, newWidth, newHeight, saveToStream);
			}
		}
		static public void SaveImage(Stream imageStream, int newNumberOfPixels, Stream saveToStream)
		{
			using (Image iOriginal = Image.Load(imageStream))
			{
				imageStream.Position = 0;
				double ratio = Math.Sqrt(newNumberOfPixels / (double)(iOriginal.Width * iOriginal.Height));
				resize(imageStream, iOriginal, (int)Math.Round(iOriginal.Width * ratio, 0), (int)Math.Round(iOriginal.Height * ratio, 0), saveToStream);
			}
		}
		static private void resize(Stream origSource, Image image, int newWidth, int newHeight, Stream saveTo)
		{
			image.Mutate(x => x.Resize(newWidth, newHeight));
			TransformImage(image); // transform image after resizing it!!!
			image.Save(saveTo, Image.DetectFormat(origSource));
		}
		private static void TransformImage(Image image)
		{
			IExifValue exifOrientation = image.Metadata?.ExifProfile?.GetValue(ExifTag.Orientation);

			if (exifOrientation == null)
				return;

			RotateMode rotateMode;
			FlipMode flipMode;
			SetRotateFlipMode(exifOrientation, out rotateMode, out flipMode);

			image.Mutate(x => x.RotateFlip(rotateMode, flipMode));
			image.Metadata.ExifProfile.SetValue(ExifTag.Orientation, (ushort)1);
		}
		private static void SetRotateFlipMode(IExifValue exifOrientation, out RotateMode rotateMode, out FlipMode flipMode)
		{
			var orientation = (ushort)exifOrientation.GetValue();

			switch (orientation)
			{
				case 2:
					rotateMode = RotateMode.None;
					flipMode = FlipMode.Horizontal;
					break;
				case 3:
					rotateMode = RotateMode.Rotate180;
					flipMode = FlipMode.None;
					break;
				case 4:
					rotateMode = RotateMode.Rotate180;
					flipMode = FlipMode.Horizontal;
					break;
				case 5:
					rotateMode = RotateMode.Rotate90;
					flipMode = FlipMode.Horizontal;
					break;
				case 6:
					rotateMode = RotateMode.Rotate90;
					flipMode = FlipMode.None;
					break;
				case 7:
					rotateMode = RotateMode.Rotate90;
					flipMode = FlipMode.Vertical;
					break;
				case 8:
					rotateMode = RotateMode.Rotate270;
					flipMode = FlipMode.None;
					break;
				default:
					rotateMode = RotateMode.None;
					flipMode = FlipMode.None;
					break;
			}
		}
		private static byte[] ImageToBytes(Image image, IImageFormat imageFormat)
		{
			using (var ms = new MemoryStream())
			{
				image.Save(ms, imageFormat);
				return ms.ToArray();
			}
		}
	}
}

Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site.
OK