/// <summary>
/// 获取原图旋转角度(IOS和Android相机拍的照片)
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static int ReadPictureDegree(string path)
{
int rotate = 0;
try
{
using (var image = System.Drawing.Image.FromFile(path, true))
{
foreach (var prop in image.PropertyItems)
{
if (prop.Id == 0x112)
{
if (prop.Value[0] == 6)
rotate = 90;
if (prop.Value[0] == 8)
rotate = -90;
if (prop.Value[0] == 3)
rotate = 180;
prop.Value[0] = 1;
}
}
}
}
catch (Exception ex)
{
LogUtility.Error("ReadPictureDegree", ex);
}
return rotate;
}
/// <summary>
/// 旋转
/// </summary>
/// <param name="path"></param>
/// <param name="rotateFlipType"></param>
/// <returns></returns>
public bool KiRotate(string path, RotateFlipType rotateFlipType)
{
try
{
using (Bitmap bitmap = new Bitmap(path))
{
bitmap.RotateFlip(rotateFlipType);
bitmap.Save(path);
}
return true;
}
catch(Exception ex)
{
return false;
}
}
解释:
顺时针旋转90度 RotateFlipType.Rotate90FlipNone
逆时针旋转90度 RotateFlipType.Rotate270FlipNone
水平翻转 RotateFlipType.Rotate180FlipY
垂直翻转 RotateFlipType.Rotate180FlipX
调用:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string path = @"C:\Users\wangbinbin\Desktop\WebApi和AccessWebApi两个项目(包含图片和文件上传)\AccessWebApi\AccessWebApi\images\25b3452f161450251612829203.jpg";
int rotate=readPictureDegree(path);
if (rotate == 90)
{
KiRotate(path, RotateFlipType.Rotate90FlipNone);
}
else if (rotate == -90)
{
KiRotate(path, RotateFlipType.Rotate270FlipNone);
}
else if (rotate == 180)
{
KiRotate(path, RotateFlipType.Rotate180FlipNone);
}
}
}
版权声明:本文为yfz19890410原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。