using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
namespace Utils
{
public static class ImageHelper
{
public const string HtmlBase64Format = "data:{0};base64,{1}";
public static string ToHtmlBase64(this Image img)
{
var mimeType = img.GetMimeType();
var base64 = img.ToBase64();
return string.Format(HtmlBase64Format, mimeType, base64);
}
public static string ToBase64(this Image img)
{
using (var ms = new MemoryStream())
{
img.Save(ms, img.RawFormat);
var imageBytes = ms.ToArray();
return Convert.ToBase64String(imageBytes);
}
}
public static string GetMimeType(this Image img)
{
var format = img.RawFormat;
var codec = ImageCodecInfo.GetImageDecoders().First(c => c.FormatID == format.Guid);
return codec.MimeType;
}
}
}