Ejemplo n.º 1
0
        private void tmrRefrashFrame_Tick(object sender, EventArgs e)
        {
            try
            {
                this.tmrRefrashFrame.Stop();

                ImageSize();

                SendMessage(m_CapHwnd, WM_CAP_GT_FRAME, 0, 0);

                SendMessage(m_CapHwnd, WM_CAP_COPY, 0, 0);

                OpenClipboard(m_CapHwnd);

                IntPtr img = GetClipboardData(2);

                CloseClipboard();

                //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m_Width, m_Height);

                //using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp))
                //{
                //    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
                //    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                //    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
                //    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;

                //    g.DrawImage(Image.FromHbitmap(img), 0, 0, m_Width, m_Height);
                //}

                //ImgWebCam.Image = bmp;

                IDataObject tempObj = Clipboard.GetDataObject();
                Image       tempImg = (System.Drawing.Bitmap)tempObj.GetData(DataFormats.Bitmap);

                ImgWebCam.Image = tempImg;

                ImgWebCam.Refresh();

                Application.DoEvents();

                if (!bStopped)
                {
                    this.tmrRefrashFrame.Start();
                }
                else
                {
                    string sysDir = "";
                    sysDir = AppDomain.CurrentDomain.BaseDirectory;
                    tempImg.Save(sysDir + "\\image\\pigeonimage.png", ImageFormat.Png);
                    this.tmrRefrashFrame.Start();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void tmrRefrashFrame_Tick(object sender, EventArgs e)
        {
            try
            {
                this.tmrRefrashFrame.Stop();

                ImageSize();

                SendMessage(m_CapHwnd, WM_CAP_GT_FRAME, 0, 0);

                SendMessage(m_CapHwnd, WM_CAP_COPY, 0, 0);

                OpenClipboard(m_CapHwnd);

                IntPtr img = GetClipboardData(2);

                CloseClipboard();

                //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m_Width, m_Height);

                //using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp))
                //{
                //    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
                //    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                //    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
                //    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;

                //    g.DrawImage(Image.FromHbitmap(img), 0, 0, m_Width, m_Height);
                //}

                //ImgWebCam.Image = bmp;

                IDataObject tempObj = Clipboard.GetDataObject();
                Image       tempImg = (System.Drawing.Bitmap)tempObj.GetData(DataFormats.Bitmap);
                ImgWebCam.Image = tempImg;
                using (Emgu.CV.Image <Bgr, byte> orignalFrame = new Image <Bgr, byte>((Bitmap)ImgWebCam.Image))
                {
                    //convert image into gray
                    Image <Bgr, byte>  image = orignalFrame.Resize(ImgWebCam.Size.Width, ImgWebCam.Size.Height, 0);
                    Image <Gray, byte> frame = image.Convert <Gray, byte>();
                    //Perform absdiff only if it's the second frame else store it in the global first frame
                    if (this.previousFrame != null)
                    {
                        //convert the previous frame to gray
                        Image <Gray, byte> previousFrameGray = previousFrame.Convert <Gray, byte>();
                        previousFrameGray = previousFrameGray.Resize(ImgWebCam.Size.Width, ImgWebCam.Size.Height, 0);
                        Image <Gray, byte> BgDifference = new Image <Gray, byte>(ImgWebCam.Width, ImgWebCam.Height);
                        CvInvoke.AbsDiff(previousFrameGray, frame, BgDifference);
                        //assign the current frame to previousFrame
                        this.previousFrame = new Image <Bgr, byte>((Bitmap)ImgWebCam.Image);
                        ImgWebCam.Image    = BgDifference.ToBitmap();
                        //ImgWebCam.Image = image.ToBitmap();
                    }
                    else
                    {
                        this.previousFrame = new Image <Bgr, byte>((Bitmap)ImgWebCam.Image);
                        this.previousFrame = this.previousFrame.Resize(ImgWebCam.Size.Width, ImgWebCam.Size.Height, 0);
                    }
                }
                ImgWebCam.Refresh();
                Application.DoEvents();

                if (!bStopped)
                {
                    this.tmrRefrashFrame.Start();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }