Пример #1
0
 private void timerPhoneWebCam_Tick(object sender, EventArgs e)
 {
     if (vidCapt == null)
     {
         if (intPtrVideo != IntPtr.Zero)                 // Release any previous buffer
         {
             Marshal.FreeCoTaskMem(intPtrVideo);
             intPtrVideo = IntPtr.Zero;
         }
         int deviceCount = VideoCapture.GetDeviceCount();
         if (deviceCount > 0)
         {
             try {
                 vidCapt = new VideoCapture(0, 640, 480, 24, pictBoxVideo);
                 //image capture will now continue below if successful
             }
             catch {
                 Phones.SetWebCamImage(IpAddressCur, null, Environment.MachineName);
                 return;                        //haven't actually seen this happen since we started properly disposing of vidCapt
             }
         }
         Phones.SetWebCamImage(IpAddressCur, null, Environment.MachineName);
     }
     if (vidCapt != null)
     {
         if (intPtrVideo != IntPtr.Zero)                 // Release any previous buffer
         {
             Marshal.FreeCoTaskMem(intPtrVideo);
             intPtrVideo = IntPtr.Zero;
         }
         Bitmap bitmapSmall = null;
         try {
             intPtrVideo = vidCapt.Click();                     //will fail if camera unplugged
             Bitmap bitmap = new Bitmap(vidCapt.Width, vidCapt.Height, vidCapt.Stride, PixelFormat.Format24bppRgb, intPtrVideo);
             bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); // If the image is upsidedown
             int w = 50;
             int h = (int)(((float)w) / 640f * 480f);
             bitmapSmall = new Bitmap(w, h);
             using (Graphics g = Graphics.FromImage(bitmapSmall)) {
                 g.DrawImage(bitmap, new Rectangle(0, 0, bitmapSmall.Width, bitmapSmall.Height));
             }
             bitmap.Dispose();
             bitmap = null;
         }
         catch {
             //bitmapSmall will remain null
             vidCapt.Dispose();
             vidCapt = null;                  //To prevent the above slow try/catch from happening again and again.
         }
         finally {
             //Marshal.FreeCoTaskMem(intPtrVideo);
         }
         if (IpAddressCur != "")               //found entry in phone table matching this machine ip.
         {
             Phones.SetWebCamImage(IpAddressCur, bitmapSmall, Environment.MachineName);
         }
         if (bitmapSmall != null)
         {
             bitmapSmall.Dispose();
             bitmapSmall = null;
         }
     }
 }