现在有一个panel,我把请求到的摄像头图像放在了panel里。
目前需要在摄像头上面放一个方框,我在panel的paint事件中加入了绘制长方形方法,防止被重绘掉private void panelPreview_Paint(object sender, PaintEventArgs e)
{
Point leftTop = this.panelPreview.Location;
int width = 300;
int height = 300;
Pen pen = new Pen(Color.Red, 2);
Graphics g = e.Graphics;
int x1 = leftTop.X;
int y1 = leftTop.Y;
int x2 = x1 + width;
int y2 = y1 + height;
Point[] points = { leftTop, new Point(x2, y1), new Point(x2, y2), new Point(x1, y2), leftTop };
g.DrawLines(pen, points);
}
页面有个按钮,开启摄像头private void bt_start_Click(object sender, EventArgs e)
{
this.panelPreview.Visible = true;
//this.pic_show.Visible = false;
int left = 0;
int top = 0;
pick = new Pick(this.pic_show.Handle, left, top, panelPreview.Width, panelPreview.Height);
pick.Start(); //Point point = this.panelPreview.Location;
//drawRect(point, 300, 300, new Pen(Color.Red, 2));
}摄像头没有开启时,会显示红色框,但是摄像头开启后,红色框就没了,有大神能帮忙解释下吗?
目前需要在摄像头上面放一个方框,我在panel的paint事件中加入了绘制长方形方法,防止被重绘掉private void panelPreview_Paint(object sender, PaintEventArgs e)
{
Point leftTop = this.panelPreview.Location;
int width = 300;
int height = 300;
Pen pen = new Pen(Color.Red, 2);
Graphics g = e.Graphics;
int x1 = leftTop.X;
int y1 = leftTop.Y;
int x2 = x1 + width;
int y2 = y1 + height;
Point[] points = { leftTop, new Point(x2, y1), new Point(x2, y2), new Point(x1, y2), leftTop };
g.DrawLines(pen, points);
}
页面有个按钮,开启摄像头private void bt_start_Click(object sender, EventArgs e)
{
this.panelPreview.Visible = true;
//this.pic_show.Visible = false;
int left = 0;
int top = 0;
pick = new Pick(this.pic_show.Handle, left, top, panelPreview.Width, panelPreview.Height);
pick.Start(); //Point point = this.panelPreview.Location;
//drawRect(point, 300, 300, new Pen(Color.Red, 2));
}摄像头没有开启时,会显示红色框,但是摄像头开启后,红色框就没了,有大神能帮忙解释下吗?
解决方案 »
- HtmlElementEventArgs e, e.BubbleEvent 为什么不能阻止事件冒泡??
- 关于insert数据时,textbox里有特殊字符如('"",)等,怎么拼写sql语句
- 读取EXCEL内容并和水晶报表绑定显示,出问题了,请教高手,谢谢
- class SampleCollection<T> 中尖括号 起什么作用
- 图片上传问题(急,请各位多指点)
- 虚心请教关于组件的定义,组件的用途,组件的开发
- webservice调用 急急。在线等
- udp广播----------如何同时传播文字和图片?
- 怎么在ListView上加垂直或水平滚动条,请大家指教
- 一个字符串操作的问题
- (分享)C# 创建文件夹并在此文件夹下创建文件(以当前时间命名)
- C#里如何做GPS轨迹图?
{
private const int WM_USER = 0x400;
private const int WS_CHILD = 0x40000000;
private const int WS_VISIBLE = 0x10000000;
private const int WM_CAP_START = WM_USER;
private const int WM_CAP_STOP = WM_CAP_START + 68;
private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
private const int WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;
private const int WM_CAP_SET_OVERLAY = WM_CAP_START + 51;
private const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;
private const int WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;
private const int WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;
private const int WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;
private const int WM_CAP_SET_SCALE = WM_CAP_START + 53;
private const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;
private IntPtr hWndC;
private bool bStat = false;
private IntPtr mControlPtr;
private int mWidth;
private int mHeight;
private int mLeft;
private int mTop; ///
/// 初始化摄像头
///
/// 控件的句柄
/// 开始显示的左边距
/// 开始显示的上边距
/// 要显示的宽度
/// 要显示的长度
public Pick(IntPtr handle, int left, int top, int width, int height)
{
mControlPtr = handle;
mWidth = width;
mHeight = height;
mLeft = left;
mTop = top;
}
[DllImport("avicap32.dll")]
private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
[DllImport("avicap32.dll")]
private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize);
[DllImport("User32.dll")]
private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
///
/// 开始显示图像
///
public void Start()
{
if (bStat)
return;
bStat = true;
byte[] lpszName = new byte[100];
hWndC = capCreateCaptureWindowA(lpszName, WS_CHILD | WS_VISIBLE, mLeft, mTop, mWidth, mHeight, mControlPtr, 0);
if (hWndC.ToInt32() != 0)
{
SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
}
else
{
throw new Exception("没有找到摄像头!");
}
return;
}
///
/// 停止显示
///
public void Stop()
{
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
bStat = false;
}
///
/// 抓图
///
/// 要保存bmp文件的路径
public void GrabImage(string path)
// public void GrabImage( )
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path); //IntPtr hWnd, int wMsg, int wParam, int lParam)
SendMessage(hWndC, WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
} ///
/// 录像
///
/// 要保存avi文件的路径
public void Kinescope(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt32());
SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
}
///
/// 停止录像
///
public void StopKinescope()
{
SendMessage(hWndC, WM_CAP_STOP, 0, 0);
}
}可能很多人都看过这个,这里面有个情况,抓图的时候,比如我界面窗口是400*400,但是抓到的图永远是640*480,也就是我界面上只显示了640*480中左上角开始400*400的部分。请问下我怎么才能抓到400*400的图呢,就是和界面上显示的一样的图片,本人对C#不是很熟悉,请大家帮忙看下