Created
November 1, 2019 20:09
-
-
Save Adebayo-Adesegun/0e13aac696c1a916d690522276ac5878 to your computer and use it in GitHub Desktop.
Implementing the Magick.NET Wrapper for .NET
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
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