首页上一页 1 下一页尾页 3 条记录 1/1页
录像,不知道为什么录不了文件
发表在C#图书答疑
2012-08-02
是否精华
是
否
版块置顶:
是
否
namespace EXT13_08
{
public partial class Form1 : Form
{
cVideo video = null;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 打开视频
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
btnPlay.Enabled = false;
btnStop.Enabled = true;
btnPz.Enabled = true;
video = new cVideo(pictureBox1.Handle, pictureBox1.Width, pictureBox1.Height);
video.StartWebCam();
}
/// <summary>
/// 关闭视频
/// </summary>
private void btnStop_Click(object sender, EventArgs e)
{
btnPlay.Enabled = true;
btnStop.Enabled = false;
btnPz.Enabled = false;
video.CloseWebcam();
}
/// <summary>
/// 拍照
/// </summary>
private void bntPz_Click(object sender, EventArgs e)
{
video.GrabImage(pictureBox1.Handle, "d:\\a.bmp");
}
/// <summary>
/// 开始录像
/// </summary>
private void btnRecord_Click(object sender, EventArgs e)
{
btnStar.Enabled = false;
btnStop.Enabled = true;
video.StarKinescope(@"d:\lx.avi");
}
}
public class cVideo //视频类
{
private IntPtr lwndC; //保存无符号句柄
private IntPtr mControlPtr; //保存管理指示器
private int mWidth;
private int mHeight;
private const int WM_USER = 0x400;
private const int WM_CAP_START = WM_USER;
private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
private const int WM_CAP_STOP = WM_CAP_START + 68;
private IntPtr hWndC;
public cVideo(IntPtr handle, int width, int height)
{
mControlPtr = handle; //显示视频控件的句柄
mWidth = width; //视频宽度
mHeight = height; //视频高度
}
/// <summary>
/// 打开视频设备
/// </summary>
public void StartWebCam()
{
byte[] lpszName = new byte[100];
byte[] lpszVer = new byte[100];
VideoAPI.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100);
this.lwndC = VideoAPI.capCreateCaptureWindowA(lpszName, VideoAPI.WS_CHILD | VideoAPI.WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0);
if (VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0))
{
VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SET_PREVIEWRATE, 100, 0);
VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SET_PREVIEW, true, 0);
}
}
/// <summary>
/// 关闭视频设备
/// </summary>
public void CloseWebcam()
{
VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_DRIVER_DISCONNECT, 0, 0);
}
/// <summary>
/// 拍照
/// </summary>
/// <param name="path">要保存bmp文件的路径</param>
public void GrabImage(IntPtr hWndC, string path)
{
IntPtr hBmp = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(path);
VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
}
/// <summary>
/// 开始录像
/// </summary>
/// <param name="path">要保存录像的路径</param>
public void StarKinescope(string path)
{
IntPtr hBmp = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(path);
VideoAPI.SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt32());
VideoAPI.SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
}
/// <summary>
/// 停止录像
/// </summary>
public void StopKinescope()
{
VideoAPI.SendMessage(hWndC, WM_CAP_STOP, 0, 0);
}
}
还需要做其他事情吗?
d盘里没有文件。
{
public partial class Form1 : Form
{
cVideo video = null;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 打开视频
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
btnPlay.Enabled = false;
btnStop.Enabled = true;
btnPz.Enabled = true;
video = new cVideo(pictureBox1.Handle, pictureBox1.Width, pictureBox1.Height);
video.StartWebCam();
}
/// <summary>
/// 关闭视频
/// </summary>
private void btnStop_Click(object sender, EventArgs e)
{
btnPlay.Enabled = true;
btnStop.Enabled = false;
btnPz.Enabled = false;
video.CloseWebcam();
}
/// <summary>
/// 拍照
/// </summary>
private void bntPz_Click(object sender, EventArgs e)
{
video.GrabImage(pictureBox1.Handle, "d:\\a.bmp");
}
/// <summary>
/// 开始录像
/// </summary>
private void btnRecord_Click(object sender, EventArgs e)
{
btnStar.Enabled = false;
btnStop.Enabled = true;
video.StarKinescope(@"d:\lx.avi");
}
}
public class cVideo //视频类
{
private IntPtr lwndC; //保存无符号句柄
private IntPtr mControlPtr; //保存管理指示器
private int mWidth;
private int mHeight;
private const int WM_USER = 0x400;
private const int WM_CAP_START = WM_USER;
private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
private const int WM_CAP_STOP = WM_CAP_START + 68;
private IntPtr hWndC;
public cVideo(IntPtr handle, int width, int height)
{
mControlPtr = handle; //显示视频控件的句柄
mWidth = width; //视频宽度
mHeight = height; //视频高度
}
/// <summary>
/// 打开视频设备
/// </summary>
public void StartWebCam()
{
byte[] lpszName = new byte[100];
byte[] lpszVer = new byte[100];
VideoAPI.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100);
this.lwndC = VideoAPI.capCreateCaptureWindowA(lpszName, VideoAPI.WS_CHILD | VideoAPI.WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0);
if (VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0))
{
VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SET_PREVIEWRATE, 100, 0);
VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SET_PREVIEW, true, 0);
}
}
/// <summary>
/// 关闭视频设备
/// </summary>
public void CloseWebcam()
{
VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_DRIVER_DISCONNECT, 0, 0);
}
/// <summary>
/// 拍照
/// </summary>
/// <param name="path">要保存bmp文件的路径</param>
public void GrabImage(IntPtr hWndC, string path)
{
IntPtr hBmp = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(path);
VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
}
/// <summary>
/// 开始录像
/// </summary>
/// <param name="path">要保存录像的路径</param>
public void StarKinescope(string path)
{
IntPtr hBmp = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(path);
VideoAPI.SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt32());
VideoAPI.SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
}
/// <summary>
/// 停止录像
/// </summary>
public void StopKinescope()
{
VideoAPI.SendMessage(hWndC, WM_CAP_STOP, 0, 0);
}
}
还需要做其他事情吗?
d盘里没有文件。