コード例 #1
0
        private void ImageCallbackFunc(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo, IntPtr pUser)
        {
            try
            {
                lock (this)
                {
                    newBmp = new Bitmap(pFrameInfo.nWidth, pFrameInfo.nHeight, PixelFormat.Format8bppIndexed);
                    // Lock the bits of the bitmap.
                    BitmapData bmpData = newBmp.LockBits(new Rectangle(0, 0, newBmp.Width, newBmp.Height), ImageLockMode.ReadWrite, newBmp.PixelFormat);
                    IntPtr     ptrBmp  = bmpData.Scan0;

                    //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(pData, 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 = newBytes;
                    this.LastBmp      = this.CurrentBmp;
                    this.CurrentBmp   = 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
            {
                BitmapGrabbed?.Invoke(null, false);
            }
        }
コード例 #2
0
        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);
            }
        }