使用c#(Windows窗体)在一个avi中捕获多个网络摄像头

时间:2009-08-06 12:50:51

标签: c# webcam capture

我需要找到一种方法,如何在C#(Windows应用程序)中使用2个或更多网络摄像头制作一个视频文件。我试图用谷歌找一些样品,但没有成功。我找到了一种如何从网络摄像头录制视频的方法(使用directshowNet,directx我可以用两个网络摄像头保存两个avi ......)

有关directShowNet的更多信息:

http://directshownet.sourceforge.net /

捕获视频的示例:

http://www.codeproject.com/KB/directx/directshownet.aspx

举一个简单的例子,我有2个网络摄像头。我需要在记录期间获取帧,将一帧粘贴到另一帧附近(制作一个图像,从两个帧中创建一个帧),然后将这些新帧插入到新的avi中。 任何的想法?是否有可能及时获得帧,并从捕获的2中创建一个新的avi?

2 个答案:

答案 0 :(得分:1)

问题有点旧(!),但以防万一:我已成功使用AForge.NET库。在其2.1.4版本中,它有一个样本,显示来自两个摄像头的视频。
您所要做的就是使用VideoCaptureDevice而不是使用默认的视频播放器控件,并在NewFrame事件中创建您想要的位图。 AForge.NET甚至可以使用AVIWriter来创建输出视频。

答案 1 :(得分:0)

这里是合并2张图片的代码

#region TakeSnap Class

    IntPtr memDc;
    [StructLayout(LayoutKind.Sequential)]
        public struct Sizes
    {
        public Int32 cx;
        public Int32 cy;

        public Sizes(Int32 x, Int32 y)
        {
            cx = x;
            cy = y;
        }
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct BLENDFUNCTION
    {
        public byte BlendOp;
        public byte BlendFlags;
        public byte SourceConstantAlpha;
        public byte AlphaFormat;
    }

    [StructLayout(LayoutKind.Sequential)]
        public struct Points
    {
        public Int32 x;
        public Int32 y;

        public Points(Int32 x, Int32 y)
        {
            this.x = x;
            this.y = y;
        }
    }

    [DllImport("gdi32.dll",EntryPoint="DeleteDC")]
    public static extern IntPtr DeleteDC(IntPtr hDc);

    [DllImport("gdi32.dll",EntryPoint="DeleteObject")]
    public static extern IntPtr DeleteObject(IntPtr hDc);

    [DllImport("gdi32.dll",EntryPoint="BitBlt")]
    public static extern bool BitBlt(IntPtr hdcDest,int xDest,
        int yDest,int wDest,int hDest,IntPtr hdcSource,
        int xSrc,int ySrc,int RasterOp);

    [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleBitmap")]
    public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,
        int nWidth, int nHeight);

    [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleDC")]
    public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

    [DllImport ("gdi32.dll",EntryPoint="SelectObject")]
    public static extern IntPtr SelectObject(IntPtr hdc,IntPtr bmp);

    [DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
    public static extern IntPtr GetDesktopWindow();

    [DllImport("user32.dll",EntryPoint="GetDC")]
    public static extern IntPtr GetDC(IntPtr ptr);

    [DllImport("user32.dll",EntryPoint="GetSystemMetrics")]
    public static extern int GetSystemMetrics(int abc);

    [DllImport("user32.dll",EntryPoint="GetWindowDC")]
    public static extern IntPtr GetWindowDC(Int32 ptr);

    [DllImport("user32.dll",EntryPoint="ReleaseDC")]
    public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDc);

    protected static IntPtr m_HBitmap;
    public const int SM_CXSCREEN=0;
    public const int SM_CYSCREEN=1;
    public const int SRCCOPY = 13369376;

    #endregion



  private IntPtr get_pointer(string path)
    {

        Bitmap bitmap=new Bitmap(path);
        IntPtr oldBits = IntPtr.Zero;
        IntPtr screenDC = GetDC(IntPtr.Zero);
        IntPtr hBitmap = IntPtr.Zero;
        memDc = CreateCompatibleDC(screenDC);

        try
        {
            Point topLoc = new Point(Left, Top);
            Sizes bitMapSize = new Sizes(bitmap.Width, bitmap.Height);
            BLENDFUNCTION blendFunc = new BLENDFUNCTION();
            Points srcLoc = new Points(0, 0);

            hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
            IntPtr pt= SelectObject(memDc, hBitmap);
            ReleaseDC(IntPtr.Zero, screenDC);               
            return memDc;
        }
        catch(Exception ex)
        {
            return this.Handle;
        }

    }       
    public  Bitmap GetDesktopImage()
    {
        try
        {               

            Sizes size; 
            IntPtr hBitmap;         
            IntPtr  hDC = get_pointer(@"C:\Documents and Settings\admin\Desktop\11_13_40_453.jpg");             
            IntPtr hMemDC = CreateCompatibleDC(hDC);
            IntPtr  hDC1 = get_pointer(@"C:\Documents and Settings\admin\Desktop\11_13_42_906.jpg");        

            size.cx = this.Width-2;
            size.cy = this.Height-22;
            hBitmap = CreateCompatibleBitmap(hDC, size.cx*2, size.cy);          
            if (hBitmap!=IntPtr.Zero)
            {
                IntPtr hOld = (IntPtr)SelectObject(hMemDC, hBitmap);                    
                BitBlt(hMemDC, 0, 0,size.cx,size.cy, hDC,0, 0,SRCCOPY); 
                BitBlt(hMemDC, size.cx, 0,size.cx,size.cy, hDC1,0, 0,SRCCOPY);
                SelectObject(hMemDC, hOld);                         
                DeleteDC(hMemDC);                   
                DeleteDC(memDc);                    
                ReleaseDC(this.Handle, hDC);
                ReleaseDC(this.Handle, hDC1);               
                Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);                 
                DeleteObject(hBitmap);              
                GC.Collect();               
                return bmp;
            }           
            return null;

        }
        catch(Exception ex)
        {           
            return null;
        }
    }


    private void Createimage()
    {
        try
        {
            Bitmap bm= GetDesktopImage();
            bm.Save(Application.StartupPath+"\\temp\\"+DateTime.Now.ToString("hh-mm-ss ff")+".Jpeg",System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }