using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Jpeg;
using System.Drawing.Imaging;
using Image = SixLabors.ImageSharp.Image;
namespace PNGMetadataViewer
{
internal static class ImageExtensions
{
#region Public Methods
///
/// Extension method that converts a Image to an byte array
///
/// The Image to convert
/// An byte array containing the JPG format Image
public static byte[] ToArray(this SixLabors.ImageSharp.Image imageIn)
{
using (MemoryStream ms = new MemoryStream())
{
imageIn.Save(ms, JpegFormat.Instance);
return ms.ToArray();
}
}
///
/// Extension method that converts a Image to an byte array
///
/// The Image to convert
///
/// An byte array containing the JPG format Image
public static byte[] ToArray(this SixLabors.ImageSharp.Image imageIn, IImageFormat fmt)
{
using (MemoryStream ms = new MemoryStream())
{
imageIn.Save(ms, fmt);
return ms.ToArray();
}
}
///
/// Extension method that converts a Image to an byte array
///
/// The Image to convert
/// An byte array containing the JPG format Image
public static byte[] ToArray(this global::System.Drawing.Image imageIn)
{
return ToArray(imageIn, ImageFormat.Png);
}
///
/// Converts the image data into a byte array.
///
/// The image to convert to an array
/// The format to save the image in
/// An array of bytes
public static byte[] ToArray(this global::System.Drawing.Image imageIn, ImageFormat fmt)
{
using (MemoryStream ms = new MemoryStream())
{
imageIn.Save(ms, fmt);
return ms.ToArray();
}
}
///
/// Extension method that converts a byte array with JPG data to an Image
///
/// The byte array with JPG data
/// The reconstructed Image
public static Image ToImage(this byte[] byteArrayIn)
{
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
Image returnImage = Image.Load(ms);
return returnImage;
}
}
public static global::System.Drawing.Image ToNetImage(this byte[] byteArrayIn)
{
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
global::System.Drawing.Image returnImage = global::System.Drawing.Image.FromStream(ms);
return returnImage;
}
}
#endregion Public Methods
}
}