using Roadie.Library.Configuration;
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Roadie.Library.Imaging
{
///
/// Processor that takes images and manipulates
///
public sealed class ImageProcessor : IDisposable
{
private IntPtr nativeResource = Marshal.AllocHGlobal(100);
///
/// Read from Configuration maximum width; if not set uses default (500)
///
public int MaxWidth => Configuration.Processing.MaxImageWidth;
/////
///// Read from Configuration image encoding; if not set uses default (Jpg Quality of 90)
/////
//public ImageEncoding ImageEncoding
//{
// get
// {
// var imageEncoding = ConfigurationManager.AppSettings["ImageProcessor:ImageEncoding"];
// if (!string.IsNullOrEmpty(imageEncoding))
// {
// return (ImageEncoding)Enum.Parse(typeof(ImageEncoding), imageEncoding);
// }
// return ImageEncoding.Jpg90;
// }
//}
private IRoadieSettings Configuration { get; }
///
/// Processor that takes images and performs any manipulations
///
public ImageProcessor(IRoadieSettings configuration)
{
Configuration = configuration;
}
///
/// Perform any necessary adjustments to file
///
/// Filename to modify
/// Success
public bool Process(string file)
{
File.WriteAllBytes(file, Process(File.ReadAllBytes(file)));
return true;
}
///
/// Perform any necessary adjustments to byte array writing modified file to filename
///
/// Filename to Write Modified Byte Array to
/// Byte Array of Image To Manipulate
/// Success
public bool Process(string filename, byte[] imageBytes)
{
File.WriteAllBytes(filename, Process(imageBytes));
return true;
}
///
/// Perform any necessary adjustments to byte array returning modified array
///
/// Byte Array of Image To Manipulate
/// Modified Byte Array of Image
public byte[] Process(byte[] imageBytes)
{
//using (var resizer = new ImageResizer(imageBytes))
//{
// return resizer.Resize(this.MaxWidth, this.ImageEncoding);
//}
return ImageHelper.ResizeImage(imageBytes, MaxWidth, MaxWidth);
}
#region IDisposable Implementation
~ImageProcessor()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
if (disposing)
{
}
if (nativeResource != IntPtr.Zero)
{
Marshal.FreeHGlobal(nativeResource);
nativeResource = IntPtr.Zero;
}
}
#endregion IDisposable Implementation
/////
///// Fetch Image from Given Url and Return Image
/////
///// FQDN of Url to Image
///// Image
//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);
// }
// }
//}
/////
///// Get all Bytes for an Image
/////
///// Image to Get Bytes For
///// Byte Array of Image
//public static byte[] ImageToByteArray(Image imageIn)
//{
// using (var ms = new MemoryStream())
// {
// imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
// return ms.ToArray();
// }
//}
}
}