private void StreamGrabber_ImageGrabbed(object sender, Basler.Pylon.ImageGrabbedEventArgs e) { if (_grabCount > 0) { _count++; if (_count > _grabCount) { //Modify.Choijh.2021.05.27.Insert Disable Stop Grab Code.Start... //Stop(); //Modify.Choijh.2021.05.27.Insert Disable Stop Grab Code.End... if (GrabDone != null) { GrabDone(); } } } Basler.Pylon.IGrabResult result = e.GrabResult; if (result.GrabSucceeded) { if (result.PixelTypeValue.IsMonoImage()) { var src = result.PixelData as byte[]; var data = new byte[src.Length]; Array.Copy(src, data, src.Length); if (ImageGrabbed != null) { ImageGrabbed( new GrabInfo( EGrabResult.Success, result.Width, result.Height, 1, data)); } return; } else { var data = new byte[result.Width * result.Height * 3]; _converter.Convert(data, result); if (ImageGrabbed != null) { ImageGrabbed( new GrabInfo( EGrabResult.Success, result.Width, result.Height, 3, data)); } return; } } if (ImageGrabbed != null) { ImageGrabbed(new GrabInfo(EGrabResult.Error)); } }
private void onIamgeGrabbed(object sender, Basler.Pylon.ImageGrabbedEventArgs e) { try { // Get the grab result. IGrabResult grabResult = e.GrabResult; if (!grabResult.GrabSucceeded) { BitmapGrabbed?.Invoke(null, false); return; } // Check if the image can be displayed. if (!grabResult.IsValid) { BitmapGrabbed?.Invoke(null, false); return; } lock (this) { newBmp = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format8bppIndexed); // Lock the bits of the bitmap. BitmapData bmpData = newBmp.LockBits(new Rectangle(0, 0, newBmp.Width, newBmp.Height), ImageLockMode.ReadWrite, newBmp.PixelFormat); // Place the pointer to the buffer of the bitmap. converter.OutputPixelFormat = PixelType.Mono8; IntPtr ptrBmp = bmpData.Scan0; converter.Convert(ptrBmp, bmpData.Stride * newBmp.Height, grabResult); //palette System.Drawing.Imaging.ColorPalette palette = newBmp.Palette; for (int i = 0; i < 256; ++i) { palette.Entries[i] = Color.FromArgb(255, i, i, i); } newBmp.Palette = palette; //convert to bytes data newBytes = new byte[bmpData.Stride * newBmp.Height]; Marshal.Copy(ptrBmp, newBytes, 0, newBytes.Length); //reverse y if (this.reverseY) { newBytes = this.bytesReverseY(newBytes, bmpData.Stride, newBmp.Height); } //copy to ptr Marshal.Copy(newBytes, 0, ptrBmp, newBytes.Length); newBmp.UnlockBits(bmpData); this.CurrentBytes = this.newBytes; this.LastBmp = this.CurrentBmp; this.CurrentBmp = this.newBmp; } BmpGrabedEvnet.Set(); this.TriggerSts.Update(true); BitmapGrabbed?.Invoke(CurrentBmp, true); BytesSaveBuffer?.Invoke(CurrentBytes); // Reduce the number of displayed images to a reasonable amount // if the camera is acquiring images very fast. this.currDisplayedTime = DateTime.Now; if (this.currDisplayedTime - this.lastDisplayedTime > TimeSpan.FromMilliseconds(70)) { this.BitmapDisplayed?.Invoke(this.CurrentBmp); this.lastDisplayedTime = this.currDisplayedTime; // the LastBmp can be dispose after BitmapDisplayed invoke execute. this.LastBmp.Dispose(); } } catch (Exception) { BitmapGrabbed?.Invoke(null, false); } }