tbf/TBF/Rig/Network/Camera/common/ImageUtils.cs

71 lines
2.5 KiB
C#
Raw Normal View History

using System;
using System.Drawing;
using Common;
namespace TBF.Rig.Network.Camera.common
{
public class ImageUtils
{
public static void Rotate(Image image, ImageRotation direction)
{
if(!(direction == ImageRotation.Deg90 || direction == ImageRotation.Deg180 || direction == ImageRotation.Deg270))
return;
if (image == null)
throw new ArgumentNullException(nameof(image));
RotateFlipType flipType =
direction == ImageRotation.Deg90 ? RotateFlipType.Rotate90FlipNone :
direction == ImageRotation.Deg180 ? RotateFlipType.Rotate180FlipNone :
direction == ImageRotation.Deg270 ? RotateFlipType.Rotate270FlipNone :
RotateFlipType.RotateNoneFlipNone;
image.RotateFlip(flipType);
}
public static Image RotateCustom(Image image, ImageRotation direction)
{
if (image == null)
throw new ArgumentNullException(nameof(image));
int newWidth = image.Width;
int newHeight = image.Height;
RotateFlipType flipType;
switch (direction)
{
case ImageRotation.Deg90:
flipType = RotateFlipType.Rotate90FlipNone;
newWidth = image.Height;
newHeight = image.Width;
break;
case ImageRotation.Deg180:
flipType = RotateFlipType.Rotate180FlipNone;
break;
case ImageRotation.Deg270:
flipType = RotateFlipType.Rotate270FlipNone;
newWidth = image.Height;
newHeight = image.Width;
break;
default:
throw new ArgumentException("Unsupported rotation direction");
}
Bitmap rotated = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(rotated))
{
g.TranslateTransform(newWidth / 2f, newHeight / 2f);
g.RotateTransform(
direction == ImageRotation.Deg90 ? 90 :
direction == ImageRotation.Deg180 ? 180 :
direction == ImageRotation.Deg270 ? 270 :
0);
g.TranslateTransform(-image.Width / 2f, -image.Height / 2f);
g.DrawImage(image, new PointF(0, 0));
}
return rotated;
}
}
}