①将图片读入bitmap中
String path = @"D:/desktop/test.jpg"
Bitmap b = new Bitmap(path, true);
②旋转图片
Graphics.RotateTransform(angle)
,旋转方向默认是顺时针旋转。public static Bitmap rotateImage(Bitmap b, float angle)
{
//创建位图用于旋转图片
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//初始化Graphics类
Graphics g = Graphics.FromImage(returnBitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
//移动坐标原点到图片中心
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
//顺时针旋转对应angle的角度
g.RotateTransform(angle);
//移动坐标原地啊为原来的位置
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
//构造图片
g.DrawImage(b, new System.Drawing.Point(0, 0));
//返回位图
return returnBitmap;
}
③完整代码测试
public static Bitmap rotateImage(Bitmap b, float angle)
{
//创建位图用于旋转图片
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//初始化Graphics类
Graphics g = Graphics.FromImage(returnBitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
//移动坐标原点到图片中心
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
//顺时针旋转对应angle的角度
g.RotateTransform(angle);
//移动坐标原地啊为原来的位置
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
//构造图片
g.DrawImage(b, new System.Drawing.Point(0, 0));
//返回位图
return returnBitmap;
}
// ocr c#入口方法
public static void Main(string[] args)
{
Console.WriteLine(1);
Bitmap b = new Bitmap(@"D:/desktop/git-space/local_baidu_ocr2/local_baidu_ocr/local_baidu_ocr/bin/images/4.jpg", true);
Bitmap ans = rotateImage(b, 90);
ans.Save("../images/6.jpg", System.Drawing.Imaging.ImageFormat.Png);
Console.ReadLine();
}
④程序结果
⑤缺陷