C#实现摄像头拍照功能 拍照按钮事件怎么写。(控件已经写好的)
思路。 --------------------编程问答-------------------- --------------------编程问答-------------------- 通用写法是用WIALib或TWIN32在WIALib下大致是这样
void _FormAddAttSelect_EventSelectAttTypeScan(string _ClientAttType)
{
_AttType = _ClientAttType;
WiaClass wiaManager = null; // WIA manager COM object
CollectionClass wiaDevs = null; // WIA devices collection COM object
ItemClass wiaRoot = null; // WIA root device COM object
CollectionClass wiaPics = null; // WIA collection COM object
ItemClass wiaItem = null; // WIA image COM object
try
{
wiaManager = new WiaClass(); // create COM instance of WIA manager
wiaDevs = wiaManager.Devices as CollectionClass; // call Wia.Devices to get all devices
if ((wiaDevs == null) || (wiaDevs.Count == 0))
{
MessageBox.Show(this, "No WIA devices found!", "WIA", MessageBoxButtons.OK, MessageBoxIcon.Stop);
//Application.Exit();
return;
}
object selectUsingUI = System.Reflection.Missing.Value; // = Nothing
wiaRoot = (ItemClass)wiaManager.Create(ref selectUsingUI); // let user select device
if (wiaRoot == null) // nothing to do
return;
// this call shows the common WIA dialog to let the user select a picture:
wiaPics = wiaRoot.GetItemsFromUI(WiaFlag.SingleImage, WiaIntent.ImageTypeColor) as CollectionClass;
if (wiaPics == null)
return;
bool takeFirst = true; // this sample uses only one single picture
foreach (object wiaObj in wiaPics) // enumerate all the pictures the user selected
{
if (takeFirst)
{
DisposeImage(); // remove previous picture
wiaItem = (ItemClass)Marshal.CreateWrapperOfType(wiaObj, typeof(ItemClass));
imageFileName = Path.GetTempFileName(); // create temporary file for image
Cursor.Current = Cursors.WaitCursor; // could take some time
this.Refresh();
wiaItem.Transfer(imageFileName, false); // transfer picture to our temporary file
LoadImgToPicBox();
//PBAtt.Image = Image.FromFile(imageFileName); // create Image instance from file
//menuFileSaveAs.Enabled = true; // enable "Save as" menu entry
takeFirst = false; // first and only one done.
}
Marshal.ReleaseComObject(wiaObj); // release enumerated COM object
}
}
catch (Exception ee)
{
MessageBox.Show(this, "Acquire from WIA Imaging failed\r\n" + ee.Message, "WIA", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}
finally
{
if (wiaItem != null)
Marshal.ReleaseComObject(wiaItem); // release WIA image COM object
if (wiaPics != null)
Marshal.ReleaseComObject(wiaPics); // release WIA collection COM object
if (wiaRoot != null)
Marshal.ReleaseComObject(wiaRoot); // release WIA root device COM object
if (wiaDevs != null)
Marshal.ReleaseComObject(wiaDevs); // release WIA devices collection COM object
if (wiaManager != null)
Marshal.ReleaseComObject(wiaManager); // release WIA manager COM object
Cursor.Current = Cursors.Default; // restore cursor
}
}
--------------------编程问答-------------------- --------------------编程问答-------------------- // 视频API调用
[DllImport("avicap32.dll")]
public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
[DllImport("avicap32.dll")]
public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);
// 常量
public const int WM_USER = 0x400;
public const int WS_CHILD = 0x40000000;
public const int WS_VISIBLE = 0x10000000;
public const int SWP_NOMOVE = 0x2;
public const int SWP_NOZORDER = 0x4;
public const int WM_CAP_DRIVER_CONNECT = WM_USER + 10;
public const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11;
public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER + 5;
public const int WM_CAP_SET_PREVIEW = WM_USER + 50;
public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
public const int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45;
public const int WM_CAP_START = WM_USER;
public const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
mControlPtr = handle; //显示视频控件的句柄
mWidth = width; //视频宽度
mHeight = height; //视频高度
/// <summary>
/// 拍照
/// </summary>
/// <param name="path">要保存bmp文件的路径</param>
public void GrabImage(IntPtr hWndC, string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
}
按钮代码
private void btnPz_Click(object sender, EventArgs e)
{
video.GrabImage(pictureBox1.Handle, "d:\\a.bmp");
}
补充:.NET技术 , C#