Implementing Live View in a WPF App for Basler Cameras (pylon SDK / C# / .NET 8)

In the previous article (Displaying a Basler Camera Snapshot in a WPF App), we captured a single frame and displayed it in a WPF Image. This time, we’ll implement a live camera preview inside a WPF window.

The live view uses an event-driven approach: we receive frames via ImageGrabbed, convert them to BitmapSource, and update an Image control. The update is performed safely via the Dispatcher to avoid blocking or crashing the UI thread.


Environment / Assumptions

  • Basler pylon Camera Software Suite (reference Basler.Pylon)
  • .NET 8 / WPF (Windows desktop)
  • Camera: acA2500-14gm (assumed Mono8) If you use a color camera, you must convert based on pixel format such as BGR8packed.

Goal

  • Live preview controlled by Connect / Start / Stop / Disconnect
  • Smooth updates by continuously refreshing a BitmapSource bound to an Image
  • Safe UI updates (always via the Dispatcher)

UI (XAML)

GUILiveSampleXAML

We add two buttons: Live (start preview) and Stop (stop preview).

The code-behind is the same as in the previous article (set DataContext to the ViewModel and disconnect on closing).


Model Side (BaslerCameraSample)

If we reuse the previous StartGrabbing() implementation, frames keep piling up in ConcurrentQueue<(DateTime, IGrabResult)> _bufferedImageQueue, which is great for async saving but makes live view unnecessarily complex.

So, we add a simple streaming method dedicated to live display:

Add/Remove event handlers

To keep the design flexible, we expose methods to attach/detach ImageGrabbed handlers:


ViewModel: Event-Driven Preview → Update BitmapSource

We continue using BaslerCameraSample, but now we update CurrentFrame whenever ImageGrabbed fires.

If performance becomes an issue, consider using WriteableBitmap rather than creating a new BitmapSource every time.


Example Run

Click Connect, then Live to start continuous preview. Click Stop to stop streaming.

GUILiveSample


Common Pitfalls & Fixes

  • Colors look wrong (color cameras) Match conversion to the camera’s pixel format (e.g., PixelType.BGR8packed and PixelFormats.Bgr24).

  • Event handler registered twice Remove (-=) once before adding (+=), as shown in Start().

  • Rendering can’t keep up with camera FPS If the camera FPS is high, you often don’t need to render every frame. You can drop frames by using Monitor.TryEnter or Interlocked.Exchange to avoid overlapping UI updates.


Summary

  • Implemented smooth live view using pylon’s event-driven acquisition
  • Updated WPF UI safely through the Dispatcher
  • The structure is flexible and works well with ROI and resolution changes
  • Combining live preview with triggers is already valuable for many lab/inspection setups

Next: HUD Overlay

In the next article, we will overlay a HUD showing values such as FPS, exposure time, and gain on top of the live preview.

コメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA