// Occurs when an image has been acquired and is ready to be processed. private void OnImageGrabbed(Object sender, ImageGrabbedEventArgs e) { if (!Dispatcher.CheckAccess()) { // If called from a different thread, we must use the Invoke method to marshal the call to the proper GUI thread. // The grab result will be disposed after the event call. Clone the event arguments for marshaling to the GUI thread. Dispatcher.BeginInvoke(new EventHandler <ImageGrabbedEventArgs>(OnImageGrabbed), sender, e.Clone()); return; } try { // Acquire the image from the camera. Only show the latest image. The camera may acquire images faster than the images can be displayed. // Get the grab result. IGrabResult grabResult = e.GrabResult; // Check if the image can be displayed. if (grabResult.IsValid) { // Reduce the number of displayed images to a reasonable amount if the camera is acquiring images very fast. if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 33) { stopWatch.Restart(); Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); // Lock the bits of the bitmap. BitmapData bmpData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); // Place the pointer to the buffer of the bitmap. converter.OutputPixelFormat = PixelType.BGRA8packed; IntPtr ptrBmp = bmpData.Scan0; converter.Convert(ptrBmp, bmpData.Stride * bitmap.Height, grabResult); //Exception handling TODO bitmap.UnlockBits(bmpData); // Assign a temporary variable to dispose the bitmap after assigning the new bitmap to the display control. //Bitmap bitmapOld = MicroVideo.Image as Bitmap; // Provide the display control with the new bitmap. This action automatically updates the display. //MicroVideo.Image = bitmap; MicroVideo.Source = ImageSourceForBitmap(bitmap); //if (bitmapOld != null) //{ // // Dispose the bitmap. // bitmapOld.Dispose(); //} } } } catch (Exception exception) { ShowException(exception); } finally { // Dispose the grab result if needed for returning it to the grab loop. e.DisposeGrabResultIfClone(); } }
/// <summary> /// 抓取到图像 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnGrabImage(object sender, ImageGrabbedEventArgs e) { //线程处理? if (InvokeRequired) { BeginInvoke(new EventHandler <ImageGrabbedEventArgs>(OnGrabImage), sender, e.Clone()); return; } try { IGrabResult grabResult = e.GrabResult; if (grabResult.IsValid) { if (!myStopWatch.IsRunning || myStopWatch.ElapsedMilliseconds > 33) { myStopWatch.Restart(); // 实例化位图对象,图像大小指定为 相机已抓取图象的大小,此处内存未被赋值 myBitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb); if (copyBitmap == null) { copyBitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb); } // 使用 BitmapData myBitmapData = myBitmap.LockBits(new Rectangle(0, 0, myBitmap.Width, myBitmap.Height), ImageLockMode.ReadWrite, myBitmap.PixelFormat); BitmapData copyBitmapData = copyBitmap.LockBits(new Rectangle(0, 0, copyBitmap.Width, copyBitmap.Height), ImageLockMode.ReadWrite, copyBitmap.PixelFormat); // 设定转换的图像像素格式 converter.OutputPixelFormat = PixelType.BGRA8packed; // 获取 锁定的图象内存指针 IntPtr ptrBmp = myBitmapData.Scan0; // 将grabResult中的数据按照指定的内存大小及像素格式转换到ptrBmp所指向(myBitmapData)的内存区域。 converter.Convert(ptrBmp, myBitmapData.Stride * myBitmap.Height, grabResult); CopyMemory(copyBitmapData.Scan0, myBitmapData.Scan0, myBitmapData.Stride * myBitmap.Height); copyBitmap.UnlockBits(copyBitmapData); // 解锁内存 myBitmap.UnlockBits(myBitmapData); //显示新的图象 //pictureBox.Image = myBitmap; pictureBox.Invalidate(); } } } catch (Exception E) { ShowException(sender, E); } finally { // Dispose the grab result if needed for returning it to the grab loop. e.DisposeGrabResultIfClone(); } }