• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c# 操作视频

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

public class VedioGrap { private static int hHwnd; //private const int port = 2000; private struct videohdr_tag { //private byte[] lpData; //private int dwBufferLength; //private int dwBytesUsed; //private int dwTimeCaptured; //private int dwUser; //private int dwFlags; //private int[] dwReserved; } private delegate bool CallBack (int hwnd, int lParam); [DllImport ("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern int capCreateCaptureWindowA ([MarshalAs (UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID); [DllImport ("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern bool capGetDriverDescriptionA (short wDriver, [MarshalAs (UnmanagedType.VBByRefStr)] ref string lpszName, int cbName, [MarshalAs (UnmanagedType.VBByRefStr)] ref string lpszVer, int cbVer); [DllImport ("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern bool DestroyWindow (int hndw); [DllImport ("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern int SendMessage (int hwnd, int wMsg, int wParam, [MarshalAs (UnmanagedType.AsAny)] object lParam); [DllImport ("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern int SetWindowPos (int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); [DllImport ("vfw32.dll")] private static extern string capVideoStreamCallback (int hwnd, videohdr_tag videohdr_tag); [DllImport ("vicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern bool capSetCallbackOnFrame (int hwnd, string s); /// <summary> /// 开始载入视频 /// </summary> public bool OpenCapture (Panel pnlVideo) { bool videostart = false; int intWidth = pnlVideo.Width; int intHeight = pnlVideo.Height; int intDevice = 0; string refDevice = intDevice.ToString (); //创建视频窗口并得到句柄 hHwnd = VedioGrap.capCreateCaptureWindowA (ref refDevice, 1342177280, 0, 0, 640, 480, pnlVideo.Handle.ToInt32 (), 0); if (SendMessage (hHwnd, 0x40a, intDevice, 0) > 0) { SendMessage (hHwnd, 0x435, -1, 0); SendMessage (hHwnd, 0x434, 0x01, 0); SendMessage (hHwnd, 0x432, -1, 0); SetWindowPos (hHwnd, 1, 0, 0, intWidth, intHeight, 6); videostart = true; } else { VedioGrap.DestroyWindow (hHwnd); videostart = false; MessageBox.Show ("加载视频失败,请检查是否有安装设备!", "拍照"); } return videostart; } /// <summary> /// 停止视频 /// </summary> public void StopVideo () { //停止视频注销视频句柄 SendMessage (hHwnd, 0x40b, 0, 0); DestroyWindow (hHwnd); } /// <summary> /// 截图 /// </summary> /// <returns></returns> public Image GrapPhoto () { Image photo = null; SendMessage (hHwnd, 0x41e, 0, 0); IDataObject obj1 = Clipboard.GetDataObject (); if (obj1.GetDataPresent (typeof (Bitmap))) { photo = (Image)obj1.GetData (typeof (Bitmap)); //截完图不能关闭视频,否则无法继续截图 //StopVideo(); } return photo; } }

VedioGrap vedioOperator = new VedioGrap (); //实例化视频类
videostart = vedioOperator.OpenCapture (pnlVideo); //打开视频
pbTakePhoto.Image = vedioOperator.GrapPhoto (); //拍照
vedioOperator.StopVideo (); Clipboard.Clear (); //关闭移除剪贴板数据,防止再次加载时保留上次头像

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#保留2位小数几种场景总结发布时间:2022-07-10
下一篇:
C#调用C++dll发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap