roadie/Roadie.Api.Library/Imaging/ImageProcessor.cs

143 lines
4.6 KiB
C#
Raw Normal View History

2018-11-04 20:33:37 +00:00
using Roadie.Library.Configuration;
2018-11-04 15:16:52 +00:00
using System;
2018-11-03 21:21:36 +00:00
using System.IO;
2018-11-04 15:16:52 +00:00
using System.Runtime.InteropServices;
2018-11-03 21:21:36 +00:00
namespace Roadie.Library.Imaging
{
/// <summary>
2019-07-03 16:21:29 +00:00
/// Processor that takes images and manipulates
2018-11-03 21:21:36 +00:00
/// </summary>
public sealed class ImageProcessor : IDisposable
{
private IntPtr nativeResource = Marshal.AllocHGlobal(100);
2019-07-03 16:21:29 +00:00
/// <summary>
/// Read from Configuration maximum width; if not set uses default (500)
/// </summary>
public int MaxWidth => Configuration.Processing.MaxImageWidth;
2018-11-04 15:16:52 +00:00
///// <summary>
///// Read from Configuration image encoding; if not set uses default (Jpg Quality of 90)
///// </summary>
//public ImageEncoding ImageEncoding
//{
// get
// {
// var imageEncoding = ConfigurationManager.AppSettings["ImageProcessor:ImageEncoding"];
// if (!string.IsNullOrEmpty(imageEncoding))
// {
// return (ImageEncoding)Enum.Parse(typeof(ImageEncoding), imageEncoding);
// }
// return ImageEncoding.Jpg90;
// }
//}
2019-07-03 16:21:29 +00:00
private IRoadieSettings Configuration { get; }
2018-11-03 21:21:36 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// Processor that takes images and performs any manipulations
2018-11-03 21:21:36 +00:00
/// </summary>
2018-11-04 20:33:37 +00:00
public ImageProcessor(IRoadieSettings configuration)
2018-11-03 21:21:36 +00:00
{
2019-07-03 16:21:29 +00:00
Configuration = configuration;
2018-11-03 21:21:36 +00:00
}
/// <summary>
2019-07-03 16:21:29 +00:00
/// Perform any necessary adjustments to file
2018-11-03 21:21:36 +00:00
/// </summary>
/// <param name="file">Filename to modify</param>
/// <returns>Success</returns>
public bool Process(string file)
{
2019-07-03 16:21:29 +00:00
File.WriteAllBytes(file, Process(File.ReadAllBytes(file)));
2018-11-03 21:21:36 +00:00
return true;
}
/// <summary>
2019-07-03 16:21:29 +00:00
/// Perform any necessary adjustments to byte array writing modified file to filename
2018-11-03 21:21:36 +00:00
/// </summary>
/// <param name="filename">Filename to Write Modified Byte Array to</param>
/// <param name="imageBytes">Byte Array of Image To Manipulate</param>
/// <returns>Success</returns>
public bool Process(string filename, byte[] imageBytes)
{
2019-07-03 16:21:29 +00:00
File.WriteAllBytes(filename, Process(imageBytes));
2018-11-03 21:21:36 +00:00
return true;
}
/// <summary>
2019-07-03 16:21:29 +00:00
/// Perform any necessary adjustments to byte array returning modified array
2018-11-03 21:21:36 +00:00
/// </summary>
/// <param name="imageBytes">Byte Array of Image To Manipulate</param>
/// <returns>Modified Byte Array of Image</returns>
public byte[] Process(byte[] imageBytes)
{
2018-11-04 15:16:52 +00:00
//using (var resizer = new ImageResizer(imageBytes))
//{
// return resizer.Resize(this.MaxWidth, this.ImageEncoding);
//}
2019-07-03 16:21:29 +00:00
return ImageHelper.ResizeImage(imageBytes, MaxWidth, MaxWidth);
2018-11-03 21:21:36 +00:00
}
#region IDisposable Implementation
~ImageProcessor()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
if (disposing)
{
}
2019-07-03 16:21:29 +00:00
2018-11-03 21:21:36 +00:00
if (nativeResource != IntPtr.Zero)
{
Marshal.FreeHGlobal(nativeResource);
nativeResource = IntPtr.Zero;
}
}
2018-11-04 15:16:52 +00:00
#endregion IDisposable Implementation
///// <summary>
///// Fetch Image from Given Url and Return Image
///// </summary>
///// <param name="url">FQDN of Url to Image</param>
///// <returns>Image</returns>
//public static Image GetImageFromUrl(string url)
//{
// HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
// using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse())
// {
// using (Stream stream = httpWebReponse.GetResponseStream())
// {
// return Image.FromStream(stream);
// }
// }
//}
///// <summary>
///// Get all Bytes for an Image
///// </summary>
///// <param name="imageIn">Image to Get Bytes For</param>
///// <returns>Byte Array of Image</returns>
//public static byte[] ImageToByteArray(Image imageIn)
//{
// using (var ms = new MemoryStream())
// {
// imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
// return ms.ToArray();
// }
//}
2018-11-03 21:21:36 +00:00
}
2018-11-04 15:16:52 +00:00
}