Skip to content

Instantly share code, notes, and snippets.

@snobu
Created December 20, 2016 19:47
Show Gist options
  • Save snobu/d4c996eeb023e533116276a93f20c04b to your computer and use it in GitHub Desktop.
Save snobu/d4c996eeb023e533116276a93f20c04b to your computer and use it in GitHub Desktop.
ImageProcessor sample transform
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using ImageProcessor;
using ImageProcessor.Imaging.Formats;
namespace imageFactoryResize
{
class Program
{
static void Main(string[] args)
{
byte[] photoBytes = File.ReadAllBytes(@"c:\tmp\tux.png");
// Format is automatically detected though can be changed.
ISupportedImageFormat format = new JpegFormat { Quality = 70 };
// Pass in 0 as either height or width to keep aspect ratio
Size size = new Size(500, 0);
using (MemoryStream inStream = new MemoryStream(photoBytes))
{
using (MemoryStream outStream = new MemoryStream())
{
// Initialize the ImageFactory using the overload to preserve EXIF metadata.
using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true))
{
// Load, resize, set the format and quality and save an image.
imageFactory.Load(inStream)
.Resize(size)
.Format(format)
.Save(outStream);
}
// write output stream to file
using (FileStream outfile = new FileStream(@"c:\tmp\out2.jpg", FileMode.Create, FileAccess.Write))
{
outStream.WriteTo(outfile);
}
Console.WriteLine("Image transform complete.");
Console.ReadKey();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment