Displaying a Basler Camera Snapshot in a WPF App (pylon SDK / C# / .NET 8)

“You have working code, but you want to preview images in a GUI as quickly as possible.” As a first step, this article builds a minimal WPF viewer with three buttons: Connect / Disconnect / Snap. With a single click, the app captures one frame and displays it in a WPF Image control.

Example run


Goal

  • Operate the camera using three buttons: Connect / Disconnect / Snap
  • Display the snapped frame in an Image
  • Ensure the camera is properly disconnected and disposed on exit (avoid resource leaks)

Project Structure

We reuse the existing BaslerCameraSample (class library) created in earlier articles (e.g., software-trigger capture) and add a new WPF application project BaslerGUISample, referencing that library.


View (XAML)

Place three buttons and an Image preview.

Designer view example:

XAML layout

In code-behind, set DataContext = new MainViewModel(); and make sure the camera disconnects in Window_Closing (shown later).


ViewModel

  • Any MVVM base is fine (BindableBase / DelegateCommand are user-defined in this article; implementation omitted)
  • Use the existing BaslerCameraSample for connect/snap/disconnect
  • Bind CurrentFrame (BitmapSource) to Image.Source
  • If the UI freezes while connecting, consider making Connect asynchronous later

(Reference) MainWindow Code-Behind


Add Disconnect() to BaslerCameraSample

Since the earlier articles focused mainly on connecting and grabbing, we now add a proper Disconnect() to ensure resources are released when the app exits.


Implementation Tips / Common Pitfalls

  • UI thread updates When you move on to event-driven continuous acquisition, ImageGrabbed is raised from a non-UI thread. In that case, update CurrentFrame via Application.Current.Dispatcher.Invoke(...) (planned for a future article).

  • Stretch behavior UniformToFill: fills without margins (may crop) Uniform: shows the entire image (may add margins)

  • Error handling If connect/snap fails, show user-friendly feedback (e.g., MessageBox) instead of silently failing.


Summary

  • Implemented a minimal WPF viewer (Connect / Disconnect / Snap / preview)
  • Used MVVM to separate View and camera-control logic; bound BitmapSource to Image.Source
  • Added Disconnect() to reliably release camera resources on exit
  • This becomes a foundation for event-driven continuous preview in the next steps

Next Article Preview

Next, we’ll explain how to continuously acquire images using event-driven grabbing while updating the UI asynchronously. We’ll also connect it with queue-based saving and OpenCV display, aiming for a practical viewer.


About the Author

@MilleVision Sharing practical knowledge on industrial cameras and machine vision development, with a Basler pylon SDK × C# series.


Full Sample Code Package

A complete C# project (including unit tests) is available on BOOTH. It includes the omitted BindableBase and DelegateCommand implementations.

  • Exposure / gain / frame rate / ROI
  • Event-driven acquisition
  • Burst capture
  • Saving and logging
  • Updates planned alongside new articles

👉 https://millevision.booth.pm/items/7316233

コメント

コメントを残す

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

CAPTCHA