using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { const int WM_CAP_DRIVER_CONNECT = 0x40a; const int WM_CAP_DRIVER_DISCONNECT = 0x40b; const int WM_CAP_EDIT_COPY = 0x41e; const int WS_CHILD = 0x40000000; const int WS_VISIBLE = 0x10000000; const int WM_CAP_GRAB_FRAME = 0x43c; [DllImport("avicap32")] static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID); [DllImport("user32")] static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam); [DllImport("user32")] static extern bool DestroyWindow(IntPtr hwnd); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { byte[] baDeviceName = new byte[1]; //建立設備的Handle IntPtr intDeviceHandle = capCreateCaptureWindowA(baDeviceName, WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, this.Handle, 0); SendMessage(intDeviceHandle, WM_CAP_DRIVER_CONNECT, 0, 0); //連接設備 if (SendMessage(intDeviceHandle, WM_CAP_GRAB_FRAME, 0, 0) > 0) //如果有截取到畫面 { if (SendMessage(intDeviceHandle, WM_CAP_EDIT_COPY, 0, 0) > 0) //如果有Copy到剪貼簿 { IDataObject MyClipboardObject = Clipboard.GetDataObject();//取得剪貼簿物件 if (MyClipboardObject.GetDataPresent(typeof(Bitmap))) //看看是不是點陣圖 { Bitmap MyBitmap = (Bitmap)MyClipboardObject.GetData(typeof(Bitmap)); //取出點陣圖 pictureBox1.Image = MyBitmap; } } } SendMessage(intDeviceHandle, WM_CAP_DRIVER_DISCONNECT, 0, 0); //設備連階斷開 } } }
肥肚子XD
參考資料:
http://itgroup.blueshop.com.tw/TimothyChi/blog?n=convew&i=10484
http://msdn.microsoft.com/en-us/library/windows/desktop/dd756879(v=vs.85).aspx