/// <summary>
/// 图片帮助类
/// </summary>
internal class MagicImageHelper
{
/// <summary>
/// 转换Jpg渐进式
/// </summary>
/// <param name="data">图片数据</param>
/// <returns></returns>
public byte[] ConvertProgressive(byte[] data)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
byte[] resData = data;
return resData;
}
/// <summary>
/// 固定宽度等比缩放图片
/// </summary>
/// <param name="data"></param>
/// <param name="width"></param>
/// <returns></returns>
public byte[] ScaleImageByWith(byte[] data, int width)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
byte[] copyData = new byte[data.Length];
data.CopyTo(copyData, 0);
using (MemoryStream sourceStream = new MemoryStream(copyData))
{
var originalImage = Image.FromStream(sourceStream);
var destHeight = originalImage.Height * width / originalImage.Width;
var resData = Zoom(data, width, destHeight);
return resData;
}
}
/// <summary>
/// 图片缩放
/// </summary>
/// <param name="data"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public byte[] Zoom(byte[] data, int width, int height)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
byte[] copyData = new byte[data.Length];
data.CopyTo(copyData, 0);
using (MemoryStream sourceStream = new MemoryStream(copyData))
{
using (var originalImage = Image.FromStream(sourceStream))
{
using (Bitmap destBitmap = new Bitmap(width, height))
{
using (Graphics g = Graphics.FromImage(destBitmap))
{
g.InterpolationMode = InterpolationMode.High;
g.SmoothingMode = SmoothingMode.HighQuality;
g.Clear(System.Drawing.Color.Transparent);
var srcRect = new Rectangle(0, 0, originalImage.Width, originalImage.Height);
var destRect = new Rectangle(0, 0, width, height);
g.DrawImage(originalImage, destRect, srcRect, GraphicsUnit.Pixel);
using (MemoryStream destStream = new MemoryStream())
{
destBitmap.Save(destStream, originalImage.RawFormat);
var result = destStream.ToArray();
return result;
}
}
}
}
}
}
/// <summary>
/// 裁剪图片
/// </summary>
/// <param name="data"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public byte[] Clip(byte[] data, int x, int y, int width, int height)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
byte[] copyData = new byte[data.Length];
data.CopyTo(copyData, 0);
using (MemoryStream sourceStream = new MemoryStream(copyData))
{
using (var originalImage = Image.FromStream(sourceStream))
{
using (Bitmap destBitmap = new Bitmap(width, height))
{
using (var g = Graphics.FromImage(destBitmap))
{
g.InterpolationMode = InterpolationMode.High;
g.SmoothingMode = SmoothingMode.HighQuality;
g.Clear(System.Drawing.Color.Transparent);
Rectangle destRect = new Rectangle(0, 0, width, height);
Rectangle srcRect = new Rectangle(x, y, width, height);
g.DrawImage(originalImage, destRect, srcRect, GraphicsUnit.Pixel);
using (MemoryStream destStream = new MemoryStream())
{
destBitmap.Save(destStream, originalImage.RawFormat);
var resData = destStream.ToArray();
return resData;
}
}
}
}
}
}
}