- C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
namespace px
{
public partial class ScriptTemplate :
{
Point PointRotate(Point center, Point p1, double angle) {
Point tmp = new Point();
if(angle>0){
double angleHude = angle * Math.PI / 180;/*角度变成弧度*/
double x1 = (p1.X - center.X) * Math.Cos(angleHude) + (p1.Y - center.Y ) * Math.Sin(angleHude) + center .X;
double y1 = -(p1.X - center.X) * Math.Sin(angleHude) + (p1.Y - center.Y) * Math.Cos(angleHude) + center.Y;
tmp.X = (int)x1;
tmp.Y = (int)y1;
}else{
angle=angle*(-1);
double angleHude = angle * Math.PI / 180;/*角度变成弧度*/
double x1 = (p1.X - center.X) * Math.Cos(angleHude) - (p1.Y - center.Y ) * Math.Sin(angleHude) + center .X;
double y1 = (p1.X - center.X) * Math.Sin(angleHude) + (p1.Y - center.Y) * Math.Cos(angleHude) + center.Y;
tmp.X = (int)x1;
tmp.Y = (int)y1;
}
return tmp;
}
Bitmap cutImage(Bitmap bmp,Point pos,int cutWidth, int cutHeight)
{
//先初始化一个位图对象,来存储截取后的图像
Bitmap bmpDest = new Bitmap(cutWidth, cutHeight, PixelFormat.Format32bppRgb);
Graphics g=Graphics.FromImage(bmpDest);
//这个矩形定义了,你将要在被截取的图像上要截取的图像区域的左顶点位置和截取的大小
Rectangle rectSource = new Rectangle(pos.X, pos.Y, cutWidth, cutHeight);
//这个矩形定义了,你将要把 截取的图像区域 绘制到初始化的位图的位置和大小
//我的定义,说明,我将把截取的区域,从位图左顶点开始绘制,绘制截取的区域原来大小
Rectangle rectDest = new Rectangle(0, 0, cutWidth, cutHeight);
//第一个参数就是加载你要截取的图像对象,第二个和第三个参数及如上所说定义截取和绘制图像过程中的相关属性,第四个属性定义了属性值所使用的度量单位
g.DrawImage(bmp, rectDest, rectSource, GraphicsUnit.Pixel);
g.Dispose();
return bmpDest;
}
public Bitmap GetROIBitmap(Bitmap bmp, int x,int y,int w,int h)
{
Rectangle r = new Rectangle(x, y, w, h);
return bmp.Clone(r, bmp.PixelFormat);
}
Bitmap CropImage(Bitmap originImage, Rectangle region)
{
Bitmap result = new Bitmap(region.Width, region.Height);
Graphics graphics = Graphics.FromImage(result);
graphics.DrawImage(originImage, new Rectangle(0, 0, region.Width, region.Height), region, GraphicsUnit.Pixel);
return result;
}
Bitmap picRotate(Bitmap bmp,double angle)
{
angle=angle*(-1);
Bitmap reBmp = new Bitmap(bmp.Width,bmp.Height);
Graphics g = Graphics.FromImage(reBmp);
// g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.TranslateTransform((float)bmp.Width/2,(float)bmp.Height/2);
g.RotateTransform((float)angle);
g.TranslateTransform(-(float)bmp.Width/2,-(float)bmp.Height/2);
g.DrawImage(bmp,new Point(0,0));
reBmp.Save(@"E:\rotate.png", System.Drawing.Imaging.ImageFormat.Jpeg);
return reBmp;
}
Bitmap picRotate3(Bitmap b,double angle){
angle = (int)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 - (int)angle);
//恢复图像在水平和垂直方向的平移
g.TranslateTransform(-center.X, -center.Y);
g.DrawImage(b, rect);
//重至绘图的所有变换
g.ResetTransform();
g.Save();
g.Dispose();
dsImage.Save(@"E:\rotate3.png", System.Drawing.Imaging.ImageFormat.Jpeg);
return dsImage;
}
Bitmap picRotate2(Bitmap bmp,double angle)
{
Bitmap img = new Bitmap(bmp.Width,bmp.Height);
angle = angle % 360;
//弧度转换
double radian = angle * Math.PI / 180.0;
double cos = Math.Cos(radian);
double sin = Math.Sin(radian);
//原图的宽和高
int w = img.Width;
int h = img.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;
//计算偏移量
System.Drawing.Point Offset = new System.Drawing.Point((W - w) / 2, (H - h) / 2);
//构造图像显示区域:让图像的中心与窗口的中心点一致
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(Offset.X, Offset.Y, w, h);
System.Drawing.Point center = new System.Drawing.Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
g.TranslateTransform(center.X, center.Y);
g.RotateTransform((float)angle);
//恢复图像在水平和垂直方向的平移
g.TranslateTransform(-center.X, -center.Y);
g.DrawImage(img, rect);
//重至绘图的所有变换
g.ResetTransform();
g.Save();
g.Dispose();
return dsImage;
}
public override void Process()
{
运行代码日志
//WriteLog("Process Begin");
Dahua.Mvp.Script.ParamProtocol.ScriptParamModel.Image inImage;
var hasGetIn_Image = TryGetInputParam("in_Image", out inImage);
if (!hasGetIn_Image)
{
throw new Exception("Get Param in_Image Error");
}
//Image实例的getBitmap()获取bitmap对象
var inBitmap = inImage.getBitmap();
int x0=inBitmap.Size.Width/2;
int y0=inBitmap.Size.Height/2;
Point rotatePoint=new Point();
rotatePoint.X=(int) x0;
rotatePoint.Y=(int) y0;
double angle;
double centerX;
double centerY;
var hasGetin_angle = TryGetInputParam("in_angle", out angle);
if (!hasGetin_angle)
{
throw new Exception("Get Param in_a Error");
}
var hasGetin_centerX = TryGetInputParam("in_centerX", out centerX);
if (!hasGetin_centerX)
{
throw new Exception("Get Param in_a Error");
}
var hasGetin_centerY = TryGetInputParam("in_centerY", out centerY);
if (!hasGetin_centerY)
{
throw new Exception("Get Param in_a Error");
}
Point centerPoint=new Point((int) centerX,(int) centerY);
Point centerPoint2=PointRotate(rotatePoint,centerPoint,angle);
double p1x;
double p1y;
var hasGetin_p1x = TryGetInputParam("in_p1x", out p1x);
if (!hasGetin_p1x)
{
throw new Exception("Get Param in_a Error");
}
var hasGetin_p1y = TryGetInputParam("in_p1y", out p1y);
if (!hasGetin_p1y)
{
throw new Exception("Get Param in_a Error");
}
Point p1Point=new Point();
p1Point.X=(int) p1x;
p1Point.Y=(int) p1y;
Point p1Point2=PointRotate(rotatePoint,p1PoiFnt,angle);
SetOutputParam("out_p1Point2X", p1Point2.X);
SetOutputParam("out_p1Point2Y", p1Point2.Y);
Bitmap bmpRotate = picRotate(inBitmap,angle);
// Bitmap bmpRotate = picRotate3(inBitmap,angle);
Rectangle cropRegion = new Rectangle(p1Point2.X, p1Point2.Y, (centerPoint2.X-p1Point2.X), (centerPoint2.Y-p1Point2.Y));
// Rectangle cropRegion = new Rectangle(p1Point2.Y, p1Point2.X, (centerPoint2.Y-p1Point2.Y), (centerPoint2.X-p1Point2.X));
// Bitmap target=CropImage(bmpRotate, cropRegion);
// Bitmap result = bmpRotate.Clone(cropRegion, bmpRotate.PixelFormat);
Bitmap result = bmpRotate.Clone(cropRegion, PixelFormat.Format24bppRgb);
Dahua.Mvp.Script.ParamProtocol.ScriptParamModel.Image outImage=new Dahua.Mvp.Script.ParamProtocol.ScriptParamModel.Image();
outImage.setImage(result);
result.Save(@"E:\a.png", ImageFormat.Png);
}
}
}