C#调用本机摄像头,但是没有反应

weixin_38055393 2016-06-21 02:53:30
下面贴的是代码,之前用VS的那个关闭调试后就不可以再调出了
using System;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
using System;
using System.Drawing;
using System.IO.Ports;
using System.Runtime.InteropServices;
using System.Windows.Forms;

/// <summary>
/// Leon's webcam mirror
/// </summary>
public partial class Form1 : Form
{

/// <summary>
/// Webcam handle.
/// </summary>
private int hHwnd;
public Form1()
{
InitializeComponent();
}
public struct videohdr_tag
{
public byte[] lpData;
public int dwBufferLength;
public int dwBytesUsed;
public int dwTimeCaptured;
public int dwUser;
public int dwFlags;
public int[] dwReserved;
}
#region P/Invoke

[DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public 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)]
public 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)]
public static extern bool DestroyWindow(int hndw);
[DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[DllImport("vfw32.dll")]
public static extern string capVideoStreamCallback(int hwnd, videohdr_tag videohdr_tag);
[DllImport("vicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool capSetCallbackOnFrame(int hwnd, string s);

#endregion
/// <summary>
/// Initialize Form1 and display the video in a panel.
/// </summary>
/// <returns></returns>
private bool InitializeForm1()
{
bool ok = false;

int intWidth = this.panel1.Width;
int intHeight = this.panel1.Height;
int intDevice = 0;
string refDevice = intDevice.ToString();

//Create vedio and get the window handle.
hHwnd = Form1.capCreateCaptureWindowA(ref refDevice, 1342177280, 0, 0, 640, 480, this.panel1.Handle.ToInt32(), 0);

if (Form1.SendMessage(hHwnd, 0x40a, intDevice, 0) > 0)
{
Form1.SendMessage(this.hHwnd, 0x435, -1, 0);
Form1.SendMessage(this.hHwnd, 0x434, 0x42, 0);
Form1.SendMessage(this.hHwnd, 0x432, -1, 0);
Form1.SetWindowPos(this.hHwnd, 1, 0, 0, intWidth, intHeight, 6);

ok = true;
}
else
{
Form1.DestroyWindow(this.hHwnd);
}

return ok;
}
/// <summary>
/// App run, then invoke the Form1 till successfully.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{


}
private void CloseForm1()
{
if (this.hHwnd > 0)
{
Form1.SendMessage(this.hHwnd, 0x40b, 0, 0);
Form1.DestroyWindow(this.hHwnd);
}
}
/// <summary>
/// when close window, destroy the Form1 window.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
CloseForm1();
}
/// <summary>
/// when window size changed, resize Form1 pic.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_SizeChanged(object sender, EventArgs e)
{
Form1.SetWindowPos(this.hHwnd, 1, 0, 0, this.Width, this.Height, 6);
}

private void groupBox1_Enter(object sender, EventArgs e)
{
groupBox1.MouseMove += groupBox1_MouseM;
}
private void groupBox1_MouseM(object sender,MouseEventArgs e)
{
/* System.Drawing.Pen myPen;

myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = groupBox1.CreateGraphics();
formGraphics.Clear(Color.BurlyWood);
formGraphics.DrawLine(myPen, 150, 150, e.X, e.Y);
formGraphics.DrawLine(myPen, 0, 150, 300, 150);
formGraphics.DrawLine(myPen, 150, 0, 150, 300);
myPen.Dispose();
formGraphics.Dispose();*/
}
private void button1_Click(object sender, EventArgs e)
{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = groupBox1.CreateGraphics();
formGraphics.DrawLine(myPen, 0,150, 300, 150);
formGraphics.DrawLine(myPen, 150, 0, 150, 300);
myPen.Dispose();
formGraphics.Dispose();
}

private void panel2_Paint(object sender, PaintEventArgs e)
{

}

private void button1_Click_1(object sender, EventArgs e)
{
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = Int32.Parse(comboBox2.Text);
serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
}

private void button2_Click(object sender, EventArgs e)
{
bool ok = false;
int i;
//while (!ok)
for(i=0;i<=5;i++)
{
ok = this.InitializeForm1();
System.Threading.Thread.Sleep(100);
}
}

private void button3_Click(object sender, EventArgs e)
{
// if (this.hHwnd > 0)
// {
Form1.SendMessage(this.hHwnd, 0x40b, 0, 0);
Form1.DestroyWindow(this.hHwnd);
// }
}
}
}

...全文
23 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧