最近有需求摄像头旋转,找了很多资料没有源代码,看了aforge.net源码,分析之后,实现了效果,分享个需要的童鞋。
public void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
{
if (image != null)
{
RotateFlipType pType = RotateFlipType.RotateNoneFlipNone;
if (dAngle == 0)
{
pType = RotateFlipType.RotateNoneFlipNone;
}
else if (dAngle == 90)
{
pType = RotateFlipType.Rotate90FlipNone;
}
else if (dAngle == 180)
{
pType = RotateFlipType.Rotate180FlipNone;
}
else if (dAngle == 270)
{
pType = RotateFlipType.Rotate270FlipNone;
}
// 实时按角度绘制
image.RotateFlip(pType);
}
}
/// <summary>
/// 旋转函数,外部调用触发
/// </summary>
public void Rotate()
{
// 计算角度,类变量
dAngle = dAngle + 90;
dAngle = dAngle % 360;
// 事件绑定,实时绘制,达到效果,本来就是实时绘制的原理
videoSourcePlayer.NewFrame -= new VideoSourcePlayer.NewFrameHandler(videoSourcePlayer_NewFrame);
videoSourcePlayer.NewFrame += new VideoSourcePlayer.NewFrameHandler(videoSourcePlayer_NewFrame);
}
public void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
{
if (image != null)
{
RotateFlipType pType = RotateFlipType.RotateNoneFlipNone;
if (dAngle == 0)
{
pType = RotateFlipType.RotateNoneFlipNone;
}
else if (dAngle == 90)
{
pType = RotateFlipType.Rotate90FlipNone;
}
else if (dAngle == 180)
{
pType = RotateFlipType.Rotate180FlipNone;
}
else if (dAngle == 270)
{
pType = RotateFlipType.Rotate270FlipNone;
}
// 实时按角度绘制
image.RotateFlip(pType);
}
}
/// <summary>
/// 旋转函数,外部调用触发
/// </summary>
public void Rotate()
{
// 计算角度,类变量
dAngle = dAngle + 90;
dAngle = dAngle % 360;
// 事件绑定,实时绘制,达到效果,本来就是实时绘制的原理
videoSourcePlayer.NewFrame -= new VideoSourcePlayer.NewFrameHandler(videoSourcePlayer_NewFrame);
videoSourcePlayer.NewFrame += new VideoSourcePlayer.NewFrameHandler(videoSourcePlayer_NewFrame);
}