Skip to content

Instantly share code, notes, and snippets.

@Adebayo-Adesegun
Created November 1, 2019 20:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adebayo-Adesegun/0e13aac696c1a916d690522276ac5878 to your computer and use it in GitHub Desktop.
Save Adebayo-Adesegun/0e13aac696c1a916d690522276ac5878 to your computer and use it in GitHub Desktop.
Implementing the Magick.NET Wrapper for .NET
public class ImageProcess
{
private string _inputPath;
private string _outputPath;
private int _resizeToHeight;
private int _resizeToWidth;
public ImageProcess(string inputPath, string outPutPath, int resizeHeight = 650, int resizeWidth = 600)
{
_inputPath = inputPath;
_outputPath = outPutPath;
_resizeToHeight = resizeHeight;
_resizeToWidth = resizeWidth;
}
/// <summary>
/// Compress Image
/// </summary>
/// <returns></returns>
public Tuple<bool, string> CompressImage()
{
try
{
using (var image = new MagickImage(_inputPath))
{
var geometry = new MagickGeometry
{
Height = _resizeToHeight,
Width = _resizeToWidth,
IgnoreAspectRatio = false,
Greater = true
};
image.Resize(geometry);
using (var stream = new MemoryStream())
{
image.Write(stream);
byte[] imageByte = stream.ToArray();
File.WriteAllBytes(_outputPath, imageByte);
}
}
return new Tuple<bool, string>(true, $"Image saved to {_outputPath}");
}
catch (MagickException ex) // Catch Magick ExceptionErrors
{
throw ex;
}
catch (Exception ex) // Catch Exception Errors
{
throw ex;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment