首页 新闻 会员 周边 捐助

C#如何在摄像头预览窗口画图

0
悬赏园豆:30 [已解决问题] 解决于 2012-12-25 13:45
我用c#写的usb摄像头程序,预览画面可以看到了,现在我想在预览画面上实时地画图,视频画面我是放在panel里的,但是鼠标在上面画图看不到画的线,有人说再在上面加个picturebox,然后在picturebox上面画,可是我试了还是不行,即使把picturebox的背景设置成透明也会挡住视频画面,所以这个方法应该行不通。请问各位高手,有没有 做过类似的程序,给小弟一些指点。谢谢
冰川人看世界的主页 冰川人看世界 | 初学一级 | 园豆:41
提问于:2012-12-13 12:03
< >
分享
最佳答案
0

一般系统视频接口或其它视频设备的SDK 是C++的。。那么在c#这边用 panel 或picturebox 都是可以的。。在Panel或Picturebox控件里有一个属性  Handle

收获园豆:30
在 水 一 方 | 小虾三级 |园豆:1097 | 2012-12-13 12:42

能说说你的思路吗?谢谢

冰川人看世界 | 园豆:41 (初学一级) | 2012-12-13 13:05
  1 一个完整的调用摄像头的示例~
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Text;
  5 using System.Runtime.InteropServices;
  6 public class VideoWork
  7 {
  8 private const int WM_USER = 0×400;
  9 private const int WS_CHILD = 0×40000000;
 10 private const int WS_VISIBLE = 0×10000000;
 11 private const int WM_CAP_START = WM_USER;
 12 private const int WM_CAP_STOP = WM_CAP_START + 68;
 13 private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
 14 private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
 15 private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
 16 private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
 17 private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
 18 private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
 19 private const int WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;
 20 private const int WM_CAP_SET_OVERLAY = WM_CAP_START + 51;
 21 private const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
 22 private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;
 23 private const int WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;
 24 private const int WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;
 25 private const int WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;
 26 private const int WM_CAP_SET_SCALE = WM_CAP_START + 53;
 27 private const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;
 28 private IntPtr hWndC;
 29 private bool bWorkStart = false;
 30 private IntPtr mControlPtr;
 31 private int mWidth;
 32 private int mHeight;
 33 private int mLeft;
 34 private int mTop;
 35 
 36 /// <summary>
 37 /// 初始化显示图像
 38 /// </summary>
 39 /// <param name= “handle “> 控件的句柄 </param>
 40 /// <param name= “left “> 开始显示的左边距 </param>
 41 /// <param name= “top “> 开始显示的上边距 </param>
 42 /// <param name= “width “> 要显示的宽度 </param>
 43 /// <param name= “height “> 要显示的长度 </param>
 44 public VideoWork(IntPtr handle, int left, int top, int width, int height)
 45 {
 46 mControlPtr = handle;
 47 mWidth = width;
 48 mHeight = height;
 49 mLeft = left;
 50 mTop = top;
 51 }
 52 
 53 [DllImport("avicap32.dll ")]
 54 private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
 55 
 56 [DllImport("avicap32.dll ")]
 57 private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize);
 58 
 59 //
 60 //这里特别注意,因为WinAPI中的long为32位,而C#中的long为64wei,所以需要将lParam该为int
 61 //
 62 [DllImport("User32.dll ")]
 63 private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
 64 
 65 /// <summary>
 66 /// 开始显示图像
 67 /// </summary>
 68 public void Start()
 69 {
 70 if (bWorkStart)
 71 return;
 72 
 73 bWorkStart = true;
 74 byte[] lpszName = new byte[100];
 75 
 76 hWndC = capCreateCaptureWindowA(lpszName, WS_CHILD | WS_VISIBLE, mLeft, mTop, mWidth, mHeight, mControlPtr, 0);
 77 
 78 if (hWndC.ToInt32() != 0)
 79 {
 80 SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
 81 SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
 82 SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
 83 SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
 84 SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);
 85 SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);
 86 SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
 87 SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
 88 //Global.log.Write( “SendMessage “);
 89 }
 90 return;
 91 
 92 }
 93 
 94 /// <summary>
 95 /// 停止显示
 96 /// </summary>
 97 public void Stop()
 98 {
 99 SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
100 bWorkStart = false;
101 }
102 
103 /// <summary>
104 /// 抓图
105 /// </summary>
106 /// <param name= “path “> 要保存bmp文件的路径 </param>
107 public void GrabImage(string path)
108 {
109 IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
110 SendMessage(hWndC, WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
111 }
112 }
113 
114 调用方法:
115 
116 在窗体上拖入一个panel
117 
118 调用代码为:
119 
120 VideoWork wv = new VideoWork(panel1.Handle, 0, 0, panel1.Width, panel1.Height);
121 wv.Start();

 

@一往而深: 

在 水 一 方 | 园豆:1097 (小虾三级) | 2012-12-13 13:26

哪里出了问题呢

诺子明 | 园豆:202 (菜鸟二级) | 2021-01-18 17:52
其他回答(1)
0

panel里显示不出来

诺子明 | 园豆:202 (菜鸟二级) | 2021-01-18 17:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册