Browse Source

DrawingUtility 增加处理图像旋转的方法,增加等比例缩放的方法。

master
Elivo 2 weeks ago
parent
commit
e984bb9dc1
  1. 99
      Apewer/DrawingUtility.cs

99
Apewer/DrawingUtility.cs

@ -25,6 +25,72 @@ namespace Apewer
return graphics;
}
/// <summary>识别图像的旋转角度并进行调整。</summary>
/// <exception cref="ArgumentNullException"></exception>
public static void AdjustRotation(this Image image)
{
if (image == null) throw new ArgumentNullException(nameof(image));
// 检测旋转角度
var angle = 0;
if (Array.IndexOf(image.PropertyIdList, 274) > -1)
{
var property = image.GetPropertyItem(274);
var value = property.Value[0];
switch (value)
{
case 1: break;
case 3: angle = 180; break;
case 6: angle = 90; break;
case 8: angle = 270; break;
}
}
// 自适应旋转
switch (angle)
{
case 90: image.RotateFlip(RotateFlipType.Rotate90FlipNone); break;
case 180: image.RotateFlip(RotateFlipType.Rotate180FlipNone); break;
case 270: image.RotateFlip(RotateFlipType.Rotate270FlipNone); break;
}
}
/// <summary>作为图像,执行回调。</summary>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public static void AsImage(this byte[] imageData, Action<Image> callback)
{
if (imageData == null) throw new ArgumentNullException(nameof(imageData));
if (callback == null) throw new ArgumentNullException(nameof(callback));
using (var stream = new MemoryStream(imageData))
{
using (var image = Image.FromStream(stream))
{
AdjustRotation(image);
callback.Invoke(image);
}
}
}
/// <summary>作为图像,执行回调。</summary>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public static TResult AsImage<TResult>(this byte[] imageData, Func<Image, TResult> callback)
{
if (imageData == null) throw new ArgumentNullException(nameof(imageData));
if (callback == null) throw new ArgumentNullException(nameof(callback));
using (var stream = new MemoryStream(imageData))
{
using (var image = Image.FromStream(stream))
{
AdjustRotation(image);
return callback.Invoke(image);
}
}
}
/// <summary>保存图像为文件。</summary>
/// <exception cref="ArgumentNullException"></exception>
public static byte[] Save(this Image image, ImageFormat format)
@ -63,6 +129,38 @@ namespace Apewer
}
}
/// <summary>等比例缩放,生成新图像。</summary>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static Image AspectScale(this Image origin, int maxSide, Color? backgroundColor = null) => AspectScale(origin, maxSide, maxSide, backgroundColor);
/// <summary>等比例缩放,生成新图像。</summary>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static Image AspectScale(this Image origin, int maxWidth, int maxHeight, Color? backgroundColor = null)
{
if (origin == null) throw new ArgumentNullException(nameof(origin));
if (maxWidth < 1) throw new ArgumentOutOfRangeException(nameof(maxWidth));
if (maxHeight < 1) throw new ArgumentOutOfRangeException(nameof(maxHeight));
var width = origin.Width;
var height = origin.Height;
var radio = Convert.ToDouble(width) / Convert.ToDouble(height);
if (width > maxWidth)
{
width = maxWidth;
height = Convert.ToInt32(maxWidth / radio);
}
if (height > maxHeight)
{
width = Convert.ToInt32(maxHeight * radio);
height = maxHeight;
}
return Scale(origin, width, height, backgroundColor);
}
/// <summary>缩放图像,生成新图像。</summary>
/// <param name="image">原始图像。</param>
/// <param name="size">新图像的大小。</param>
@ -102,6 +200,7 @@ namespace Apewer
if (isTransparentFormat)
{
if (backgroundColor != null && backgroundColor.HasValue) graphic.Clear(backgroundColor.Value);
else graphic.Clear(Color.Transparent);
}
// 绘制新图像

Loading…
Cancel
Save