方法一:
public static Bitmap rotateImage(Bitmap b, float angle)
{
//create a new empty bitmap to hold rotated image
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(returnBitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
//move rotation point to center of image
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
//rotate
g.RotateTransform(angle);
//move image back
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
//draw passed in image onto graphics object
g.DrawImage(b, new Point(0, 0));
return returnBitmap;
}
方法二:
public static Bitmap RotateImage(Bitmap image, float rotateAtX, float rotateAtY, float angle, bool bNoClip)
{
int W, H, X, Y;
if (bNoClip)
{
double dW = (double)image.Width;
double dH = (double)image.Height;
double degrees = Math.Abs(angle);
if (degrees <= 90)
{
double radians = 0.0174532925 * degrees;
double dSin = Math.Sin(radians);
double dCos = Math.Cos(radians);
W = (int)(dH * dSin + dW * dCos);
H = (int)(dW * dSin + dH * dCos);
X = (W - image.Width) / 2;
Y = (H - image.Height) / 2;
}
else
{
degrees -= 90;
double radians = 0.0174532925 * degrees;
double dSin = Math.Sin(radians);
double dCos = Math.Cos(radians);
W = (int)(dW * dSin + dH * dCos);
H = (int)(dH * dSin + dW * dCos);
X = (W - image.Width) / 2;
Y = (H - image.Height) / 2;
}
}
else
{
W = image.Width;
H = image.Height;
X = 0;
Y = 0;
}
//create a new empty bitmap to hold rotated image
Bitmap bmpRet = new Bitmap(W, H);
bmpRet.SetResolution(image.HorizontalResolution, image.VerticalResolution);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(bmpRet);
//Put the rotation point in the "center" of the image
g.TranslateTransform(rotateAtX + X, rotateAtY + Y);
//rotate the image
g.RotateTransform(angle);
//move the image back
g.TranslateTransform(-rotateAtX - X, -rotateAtY - Y);
//draw passed in image onto graphics object
g.DrawImage(image, new PointF(0 + X, 0 + Y));
return bmpRet;
}
方法三:
#region 图片旋转函数
/// <summary>
/// 以逆时针为方向对图像进行旋转
/// </summary>
/// <param name="b">位图流</param>
/// <param name="angle">旋转角度[0,360](前台给的)</param>
/// <returns></returns>
public static Bitmap Rotate(Bitmap b, int angle)
{
angle = angle % 360;
//弧度转换
double radian = angle * Math.PI / 180.0;
double cos = Math.Cos(radian);
double sin = Math.Sin(radian);
//原图的宽和高
int w = b.Width;
int h = b.Height;
int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
//目标位图
Bitmap dsImage = new Bitmap(W, H);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//计算偏移量
Point Offset = new Point((W - w) / 2, (H - h) / 2);
//构造图像显示区域:让图像的中心与窗口的中心点一致
Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
g.TranslateTransform(center.X, center.Y);
g.RotateTransform(360 - angle);
//恢复图像在水平和垂直方向的平移
g.TranslateTransform(-center.X, -center.Y);
g.DrawImage(b, rect);
//重至绘图的所有变换
g.ResetTransform();
g.Save();
g.Dispose();
//dsImage.Save("yuancd.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
return dsImage;
}
#endregion 图片旋转函数
转载至:https://www.cnblogs.com/xqaizx/p/6598707.html
旋转图片90的整数倍那真是太简单了。
旋转的倍数函数
public static Bitmap KiRotate90(Bitmap img)
{
try
{
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
return img;
}
catch
{
return null;
}
}
namespace System.Drawing
{
//
// 摘要:
// 指定多少图像的旋转和翻转图像所用于的轴。
public enum RotateFlipType
{
//
// 摘要:
// 指定没有顺时针旋转和翻转。
RotateNoneFlipNone = 0,
//
// 摘要:
// 指定后接水平和垂直的 180 度顺时针旋转翻转。
Rotate180FlipXY = 0,
//
// 摘要:
// 指定不进行翻转顺时针旋转 90 度。
Rotate90FlipNone = 1,
//
// 摘要:
// 指定后接水平和垂直的 270 度顺时针旋转翻转。
Rotate270FlipXY = 1,
//
// 摘要:
// 指定不进行翻转 180 度的顺时针旋转。
Rotate180FlipNone = 2,
//
// 摘要:
// 指定没有顺时针旋转后接水平和垂直翻转。
RotateNoneFlipXY = 2,
//
// 摘要:
// 指定不进行翻转顺时针旋转 270 度。
Rotate270FlipNone = 3,
//
// 摘要:
// 指定旋转 90 度顺时针旋转后接水平和垂直翻转。
Rotate90FlipXY = 3,
//
// 摘要:
// 指定没有跟水平翻转的顺时针旋转。
RotateNoneFlipX = 4,
//
// 摘要:
// 指定垂直翻转后跟 180 度顺时针旋转。
Rotate180FlipY = 4,
//
// 摘要:
// 指定后接水平翻转的 90 度的顺时针旋转。
Rotate90FlipX = 5,
//
// 摘要:
// 指定垂直翻转后跟的 270 度顺时针旋转。
Rotate270FlipY = 5,
//
// 摘要:
// 指定后接水平翻转的 180 度顺时针旋转。
Rotate180FlipX = 6,
//
// 摘要:
// 指定垂直翻转后跟没有顺时针旋转。
RotateNoneFlipY = 6,
//
// 摘要:
// 指定后接水平翻转的 270 度顺时针旋转。
Rotate270FlipX = 7,
//
// 摘要:
// 指定垂直翻转后跟 90 度顺时针旋转。
Rotate90FlipY = 7
}
}