怎么实现摄像头全屏
如下代码怎么让摄像头全屏
form1.cs
private void Form1_Load(object sender, EventArgs e)
{
w = Screen.AllScreens[0].Bounds.Width;
h = Screen.AllScreens[0].Bounds.Height;
b_play.Enabled = false;
b_stop.Enabled = true;
panelPreview.Size = new Size(1024, 768);
wc = new WebCamera(panelPreview.Handle, panelPreview.Width, panelPreview.Height);
wc.StartWebCam();
}
showvideo.cs
public void StartWebCam()
{
byte[] lpszName = new byte[100];
byte[] lpszVer = new byte[100];
showVideo.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100);
this.lwndC = showVideo.capCreateCaptureWindowA(lpszName, showVideo.WS_VISIBLE | showVideo.WS_CHILD, 0, 0, mWidth, mHeight, mControlPtr, 0);
if (this.capDriverConnect(this.lwndC, 0))
{
this.capPreviewRate(this.lwndC, 66);
this.capPreview(this.lwndC, true);
showVideo.bitmapinfo bitmapinfo = new showVideo.bitmapinfo();
bitmapinfo.bmiHeader.biSize = showVideo.SizeOf(bitmapinfo.bmiHeader);
bitmapinfo.bmiHeader.biWidth = 352;
bitmapinfo.bmiHeader.biHeight = 288;
bitmapinfo.bmiHeader.biPlanes = 1;
bitmapinfo.bmiHeader.biBitCount = 24;
this.capSetVideoFormat(this.lwndC, ref bitmapinfo, showVideo.SizeOf(bitmapinfo));
this.mFrameEventHandler = new showVideo.FrameEventHandler(FrameCallBack);
this.capSetCallbackOnFrame(this.lwndC, this.mFrameEventHandler);
showVideo.SetWindowPos(this.lwndC, 0, 0, 0, mWidth, mHeight, 6);
}
}
[解决办法]
我晕
wc = new WebCamera(panelPreview.Handle, panelPreview.Width, panelPreview.Height);
你这个传递的参数是:预览框句柄,预览框宽,预览框高
你只需要将预览框设置成和屏幕同样大小,就OK
[解决办法]
this.lwndC = showVideo.capCreateCaptureWindowA(lpszName, showVideo.WS_VISIBLE | showVideo.WS_CHILD, 0, 0, mWidth, mHeight, mControlPtr, 0);
mWidth 宽
mHeight 高
[解决办法]
得到视频大小后,resize到新的picturebox里
[解决办法]
/* * Title: Webcam Monitor * Author: Ahsun * Date: April 22, 2010 * Email: ahsun.chen@gmail.com * MSN: ahsun.chen@hotmail.com */using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Threading;using System.IO;namespace WebcamMini{ public partial class Webcam : Form { public Webcam() { InitializeComponent(); } const short WM_CAP = 1024; const int WM_CAP_DRIVER_CONNECT = WM_CAP + 10; const int WM_CAP_DRIVER_DISCONNECT = WM_CAP + 11; const int WM_CAP_EDIT_COPY = WM_CAP + 30; const int WM_CAP_SET_PREVIEW = WM_CAP + 50; const int WM_CAP_SET_PREVIEWRATE = WM_CAP + 52; const int WM_CAP_SET_SCALE = WM_CAP + 53; const int WS_CHILD = 1073741824; const int WS_VISIBLE = 268435456; const short SWP_NOMOVE = 2; const short SWP_NOSIZE = 1; const short SWP_NOZORDER = 4; const short HWND_BOTTOM = 1; int iDevice = 0; int hHwnd; [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SendMessageA")] static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam); [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetWindowPos")] static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); [System.Runtime.InteropServices.DllImport("user32")] static extern bool DestroyWindow(int hndw); [System.Runtime.InteropServices.DllImport("avicap32.dll")] static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID); [System.Runtime.InteropServices.DllImport("avicap32.dll")] static extern bool capGetDriverDescriptionA(short wDriver, string lpszName, int cbName, string lpszVer, int cbVer); private void OpenPreviewWindow() { //注意我下面的picCapuure是一个pictureBox,它的的Dock属性是Fill的,这样就可以自动的全屏了,而且可以自己拖动大小 int iHeight = this.picCapture.Height ; // 高 int iWidth = this.picCapture.Width; // 宽 // // Open Preview window in picturebox // hHwnd = capCreateCaptureWindowA(iDevice.ToString(), (WS_VISIBLE | WS_CHILD), 0, 0, 640, 480, picCapture.Handle.ToInt32(), 0); // // Connect to device // if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) == 1) { // // Set the preview scale // SendMessage(hHwnd, WM_CAP_SET_SCALE, 1, 0); // // Set the preview rate in milliseconds // SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0); // // Start previewing the image from the camera // SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, 0); // // Resize window to fit in picturebox // SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, iWidth, iHeight, (SWP_NOMOVE | SWP_NOZORDER)); } else { // // Error connecting to device close window // DestroyWindow(hHwnd); } } private void ClosePreviewWindow() { // // Disconnect from device // SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0); // // close window // DestroyWindow(hHwnd); } private void TakePhotos() { // TODO: .... } //FileStream Files = new FileStream(@"D:/a.jpg", FileMode.Create); //Bitmap bmpobj = new Bitmap(200, 200); //Graphics g; //Pen RedPen = new Pen(Color.Red, 1); //bool bdrawing; //int m_MouseX, m_MouseY; private void Webcam_Load(object sender, EventArgs e) { OpenPreviewWindow(); //g = Graphics.FromImage(bmpobj); //g.Clear(Color.White); } private void Webcam_Resize(object sender, EventArgs e) { if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized) { this.Visible = false; } else { this.Visible = true; } } private void notifyIcon1_DoubleClick(object sender, EventArgs e) { this.Visible = true; this.WindowState = System.Windows.Forms.FormWindowState.Normal; } private void Webcam_SizeChanged(object sender, EventArgs e) { ClosePreviewWindow(); OpenPreviewWindow(); } private void Webcam_KeyPress(object sender, KeyPressEventArgs e) { //MessageBox.Show("heh"); //Bitmap bmp = new Bitmap(picCapture.Width, picCapture.Height); //Graphics g = Graphics.FromImage(bmp); //picCapture.Image = bmp; //bmp.Save("D:\\test.jpg"); //picCapture.Image.Save("D:\\test.jpg"); //System.Drawing.Image bitmap = new System.Drawing.Bitmap(picCapture.Width, picCapture.Height); //bmpobj.Save(Files, System.Drawing.Imaging.ImageFormat.Jpeg); //bmpobj.Dispose(); //g.Dispose(); } }}
[解决办法]
上面是我改网上的一个例子,相信能帮到LZ给lz一些想法
[解决办法]
留个记号
[解决办法]
学习中
[解决办法]
学习了,帮顶!
[解决办法]
学习了,帮顶!